android– 通过相机使用动态坐标捕获图像

android– 通过相机使用动态坐标捕获图像,第1张

概述我正在制作一个基于相机的应用程序,我在相机上放置一个矩形视图.当我使用新的Camera.PictureCallback()捕获图像时,我裁剪了该图像,因为它将获得矩形的一部分.好吧,它的工作正常.现在我实现了View.OnTouchListener并使用它使形状可移动.因此,我需要使用用户的最终选择来捕获图像

我正在制作一个基于相机的应用程序,我在相机上放置一个矩形视图.
当我使用新的Camera.PictureCallback()捕获图像时,我裁剪了该图像,因为它将获得矩形的一部分.
好吧,它的工作正常.

现在我实现了VIEw.OntouchListener并使用它使形状可移动.

因此,我需要使用用户的最终选择来捕获图像,例如他们放置矩形的位置.

Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length); // 2560×1440                float scale = 1280 / 1000;                int left = (int) (scale * (imageOriginal.getWIDth() - 250) / 2);                int top = (int) (scale * (imageOriginal.getHeight() - 616) / 2);                int wIDth = (int) (scale * 750);                int height = (int) (scale * 616);                Bitmap imageConverted = Bitmap.createBitmap(imageOriginal, left, top, wIDth, height, null, false);

这是我用来裁剪图像的方法.值很难找到确切的位置.
现在我需要改变矩形的顶部,底部,高度,宽度的值.

//我用于绘制该矩形的customVIEw

public class CustomVIEw extends VIEw  {    private Paint paint = new Paint();    public CustomVIEw(Context context) {        super(context);    }    @OverrIDe    protected voID onDraw(Canvas canvas) { // OverrIDe the onDraw() Method        super.onDraw(canvas);        paint.setStyle(Paint.Style.stroke);        paint.setcolor(color.GREEN);        paint.setstrokeWIDth(10);        //center        int x0 = canvas.getWIDth()/2;        int y0 = canvas.getHeight()/2;        int dx = canvas.getHeight()/3;        int dy = canvas.getHeight()/3;        //draw guIDe Box        canvas.drawRect(x0-dx, y0-dy, x0+dx, y0+dy, paint);    }}

//我的图片回调代码

  Camera.PictureCallback mPicture = new Camera.PictureCallback() {        public voID onPictureTaken(byte[] data, Camera camera) {            // Replacing the button after a photho was taken.            // file name of the image that we just took.            filename = "img_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()).toString() + ".jpg";            // Creating the directory where to save the image. Sadly in older            // version of AndroID we can not get the Media catalog name            file mkDir = new file(sdRoot, dir);            mkDir.mkdirs();            // Main file where to save the data that we recive from the camera            file picturefile = new file(sdRoot, dir + filename);            // CropPing image with the corresponding co-ordinates and save in to a file            try {                Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length); // 2560×1440                float scale = 1280 / 1000;                int left = (int) (scale * (imageOriginal.getWIDth() - 250) / 2);                int top = (int) (scale * (imageOriginal.getHeight() - 616) / 2);                int wIDth = (int) (scale * 750);                int height = (int) (scale * 616);                Bitmap imageConverted = Bitmap.createBitmap(imageOriginal, left, top, wIDth, height, null, false);                fileOutputStream purge = new fileOutputStream(picturefile);                imageConverted.compress(Bitmap.CompressFormat.JPEG, 100, purge);                purge.flush();                purge.close();            } catch (fileNotFoundException e) {                Log.d("DG_DEBUG", "file not found: " + e.getMessage());            } catch (IOException e) {                Log.d("DG_DEBUG", "Error accessing file: " + e.getMessage());            }            // Adding Exif data for the orIEntation.            try {                ProjectManager.getInstance().settings.IMAGE_LOCATION = "/sdcard/" + dir + filename;                exif = new ExifInterface(ProjectManager.getInstance().settings.IMAGE_LOCATION);                exif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + orIEntation);                exif.saveAttributes();                mVIEw.saveImage(dir + filename);            } catch (IOException e) {                e.printstacktrace();            }        }    };

解决方法:

对我来说,我拍摄的图像然后根据图像所选区域的尺寸进行裁剪.

public voID onPictureTaken(byte[] data, Camera camera) {                Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length, null);                int wIDth = imageOriginal.getWIDth();                int height = imageOriginal.getHeight(); // for wIDth                int narrowSize = Math.min(wIDth, height); // for height                int differ = (int) Math.abs((imageOriginal.getHeight() - imageOriginal.getWIDth()) / 2.0f);  // for dimension                wIDth = (wIDth == narrowSize) ? 0 : differ;                height = (wIDth == 0) ? differ : 0;                 Matrix rotationMatrix = new Matrix();                rotationMatrix.postRotate(90); // for orIEntation                Bitmap imageCropped = Bitmap.createBitmap(imageOriginal, wIDth, height, narrowSize, narrowSize, rotationMatrix, false);    }

Capture Image

Output Image

总结

以上是内存溢出为你收集整理的android – 通过相机使用动态坐标捕获图像全部内容,希望文章能够帮你解决android – 通过相机使用动态坐标捕获图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100035.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存