其他分享
首页 > 其他分享> > android-在camera2上裁剪矩形子部分,并将其传递给opencv进行处理

android-在camera2上裁剪矩形子部分,并将其传递给opencv进行处理

作者:互联网

我设法绘制了一个覆盖矩形,以便矩形内的内容是唯一要处理的矩形,应用程序应该忽略其余的相机区域.link to Screenshot

问题是我想在onimagaavailable函数上裁剪上面的矩形,如下所示:

@Override
    public void onImageAvailable(ImageReader reader) {
        Image img = reader.acquireLatestImage();

       //THIS IS WHERE I WANT TO CROP THE IMAGE TO ONLY SHOW WHAT WAS IN THE 
       //   ABOVE DRAWN RECTANGLE BUT I DONT KNOW HOW, I HAVE GOOGLED FOR THE 
       // PAST WEEK ON THIS


      //after processing i should pass the image to my opencv processing 
       //algorithm   
       process(img)
    }

这是我在camera2顶部绘制矩形叠加层的代码

 @Override
 protected void onDraw(Canvas canvas) { // Override the onDraw() Method
    super.onDraw(canvas);

    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(10);
    int width = canvas.getWidth() / 2;
    int height = canvas.getHeight() / 4;
    int height1 = canvas.getHeight() / 3;
    int height_1 = canvas.getHeight() / 10;

    int fin = canvas.getWidth() + canvas.getWidth() / 3;
    int top = height / 4;

    Log.e("top", top + "");
    canvas.drawRect(canvas.getWidth() - (canvas.getWidth() - 150), 
    canvas.getHeight() - fin, canvas.getWidth() - 150, height + top, paint);

}

请帮助/告诉我可以使用哪些代码从ImageReader图像中裁剪矩形,以便将裁剪后的图像传递给我的处理算法

解决方法:

在onImageAvailable()中,您可以使用
Bitmap.createBitmap(如果您已将其转换为JPEG格式)

Rect rectCrop = new Rect(left,top,right,bottom);
Bitmap bitmapRotated = Bitmap.createBitmap(bitmap, rectCrop.left, rectCrop.top, rectCrop.width(), rectCrop.height(), mat, true);

如果您使用YUV,则可以减少内存,并使用带有矩形的compressToJpeg,此过程更加轻松

yuv.compressToJpeg(rect, compress, out);

标签:image-recognition,rect,crop,camera2,android
来源: https://codeday.me/bug/20191111/2019047.html