
我正在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 总结 以上是内存溢出为你收集整理的需要在Android中使用相机捕获矩形视图内的图像全部内容,希望文章能够帮你解决需要在Android中使用相机捕获矩形视图内的图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)