gpt4 book ai didi

android - 在camera2上裁剪矩形分段,传给opencv处理

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:56 25 4
gpt4 key购买 nike

我已经设法绘制了一个覆盖矩形,以便矩形内的内容是唯一要处理的内容,应用程序应该忽略相机区域的其余部分。 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);

关于android - 在camera2上裁剪矩形分段,传给opencv处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44248186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com