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

3
我正在开发一款基于摄像头的应用程序,在摄像头上放置一个矩形视图。当我使用new Camera.PictureCallback()拍摄一张照片时,我会裁剪该图像,以便它只包含矩形部分。目前这个功能已经正常工作。
现在我实现了View.OnTouchListener并将其用于使该形状可移动。
因此,我需要捕捉用户最终选择的图像,即他们放置矩形的位置。具体效果如下图所示:This is how it looks
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);

这是我用来裁剪图像的方法。数值被硬编码以找到确切的位置。 现在,我需要随着矩形变化而获得 top、bottom、height 和 width 的数值。
//我的自定义视图用于绘制该矩形
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();
            }

        }
    };

无论用户将绿色矩形放在哪里,我都需要捕获或保存该部分。 - Varun Chandran
翻译:绿色矩形位于相机视图之上。背景是一个正在运行的相机。 - Varun Chandran
没有回复?为什么? - Varun Chandran
嗨@Varun Chandran..你到目前为止找到任何解决方案了吗? - Vibhor Bhardwaj
@VibhorBhardwaj 下面的答案给出了类似的输出,但对于我的问题并不完美。 - Varun Chandran
2个回答

2

对于我来说,我会先获取一张图片,然后根据所选区域的尺寸进行裁剪。

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);


    }

捕获图像

输出图像


0

如果我理解你的帖子正确,那么你的矩形宽度为mCustomView.getWidth()*2/3像素,高度为mCustomView.getHeight()*2/3像素,其左上角位于mCustomView.getWidth()的1/6处。

public void onPictureTaken(byte[] data, Camera camera) {
    //.../
    Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length);
    int[] customViewPosition;
    mCustomView.getLocationInWindow(customViewPosition);
    int[] surfacePosition;
    mSurfaceView.getLocationInWindow(surfacePosition);

    float scale = imageOriginal.getWidth()/(float)mSurfaceView.getWidth();
    int left = (int) scale*(customViewPosition[0] + mCustomView.getWidth()/6F - surfacePosition[0]);
    int top = (int) scale*(customViewPosition[1] + mCustomView.getHeight()/6F - surfacePosition[1]);
    int width = (int) scale*mCustomView.getWidth()*2/3;
    int height = (int) scale*mCustomView.getHeight()*2/3;
    Bitmap imageCropped = Bitmap.createBitmap(imageOriginal, left, top, width, height, null, false);
    //.../
}

嗨,我尝试了这个,但是customViewPosition和surfacePosition总是返回0,0? - Varun Chandran
我有一个疑问?SurfaceView 会一直保持不变,对吗?只有 CustomView 会改变? - Varun Chandran
customViewPosition和surfacePosition可能是(0,0)。但这取决于您的布局。mCustomView很可能也保持不变。如果我经常使用它们,我会将所有值都预先缓存。但是,当我们每次只需要它们一次来捕获图像时,开销是可以忽略的。 - Alex Cohn
无论如何,customViewPosition和surfacePosition始终为0,0。我尝试了很多次,但值没有改变。如果它们变成0,0,应用程序就会崩溃。 - Varun Chandran
1
左边=142,顶部=80,宽度=568,高度=320 ---- Bitmap imageCropped = Bitmap.createBitmap(imageOriginal,left,top,width,height,null,false); 在此语句中崩溃并显示错误-- java.lang.IllegalArgumentException:x + width必须<= bitmap.width() - Varun Chandran
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接