需要在Android中使用相机捕获矩形视图内的图像
作者:互联网
我正在android中创建一个文档扫描应用程序,正在我的项目中使用OpenCV和扫描库进行裁剪,我已经在相机视图中使用drawrect创建了一个矩形,现在我只需要捕获该矩形部分内的图像并在另一个活动中显示它.
有问题的图片:
解决方法:
对我来说,我将拍摄整个图像,然后进行裁剪.
您的问题是:“我怎么知道图像的哪个部分在矩形部分内,那么只有我能通过它,希望您能理解”.我的答案是您可以使用整个图像尺寸和相机显示屏尺寸的相对缩放.然后,您将知道要裁剪矩形的哪一部分.
这是代码示例.
请注意,您需要填写一些代码以使其可以将文件保存为jpg,并在裁剪后保存.
// 1. Save your bitmap to file
public class MyPictureCallback implements Camera.PictureCallback {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
//mPictureFile is a file to save the captured image
FileOutputStream fos = new FileOutputStream(mPictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
}
}
}
// Somewhere in your code
// 2.1 Load bitmap from your .jpg file
Bitmap bitmap = BitmapFactory.decodeFile(path+"/mPictureFile_name.jpg");
// 2.2 Rotate the bitmap to be the same as display, if need.
... Add some bitmap rotate code
// 2.3 Size of rotated bitmap
int bitWidth = bitmap.getWidth();
int bitHeight = bitmap.getHeight();
// 3. Size of camera preview on screen
int preWidth = preview.getWidth();
int preHeight = preview.getHeight();
// 4. Scale it.
// Assume you draw Rect as "canvas.drawRect(60, 50, 210, 297, paint);" command
int startx = 60 * bitWidth / preWidth;
int starty = 50 * bitHeight / preHeight;
int endx = 210 * bitWidth / preWidth;
int endy = 297 * bitHeight / preHeight;
// 5. Crop image
Bitmap blueArea = Bitmap.createBitmap(bitmap, startx, starty, endx, endy);
// 6. Save Crop bitmap to file
标签:opencv,android-camera,android 来源: https://codeday.me/bug/20191118/2031432.html