bal*_*nav 3 android opencv android-camera
我在 android 中创建一个文档扫描应用程序,在我的项目中使用 OpenCV 和 Scan 库进行裁剪,我在相机视图中使用 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8265 次 |
| 最近记录: |