简体   繁体   English

Android裁剪相机图片

[英]Android crop camera image

I'm building a camera app similar to barcode scanner shown in picture. 我正在构建类似于图片中所示的条形码扫描仪的相机应用程序。 I need to use both Camera and android.hardware.camera2 API to support android API >= 19. 我需要同时使用Cameraandroid.hardware.camera2 API来支持android API> = 19。

The main idea is to have camera preview on the whole screen, but save only part that isn't darkened(or take picture of area that isn't darkened). 主要思想是在整个屏幕上预览相机,但只保存未变暗的部分(或拍摄未变暗的区域的照片)。 I've already done some research and figured out that it's impossible to take only part of the picture through Camera and android.hardware.camera2 APIs(but I'm not 100% sure). 我已经做过一些研究,发现无法通过Cameraandroid.hardware.camera2 API仅仅拍摄部分照片(但我不确定100%)。 So I think I need to make some manipulations over the byte array(compressed into JPEG) which I receive as a result of camera capture. 因此,我认为我需要对由于摄像机捕获而收到的字节数组(压缩为JPEG)进行一些操作。

I tried to use BitmapRegionDecoder in this way: 我试图以这种方式使用BitmapRegionDecoder:

BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(bytes, 0, bytes.length, false);
Bitmap bitmap = decoder.decodeRegion(new Rect(500, 500, 1500, 1500), null);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);

Where bytes is JPEG compressed result of camera capture. 其中bytes是摄像机捕获的JPEG压缩结果。 But this approach changes the orientation of the image and doesn't crop region defined by Rect (actually crops another part of the image) 但是这种方法会改变图像的方向,并且不会裁剪Rect定义的区域(实际上会裁剪图像的另一部分)

I would appreciate any suggestions and hints on how to achieve desired behavior. 我将不胜感激有关如何实现所需行为的任​​何建议和提示。

在此处输入图片说明

It has been a while, but people still interested in this question. 已经有一段时间了,但是人们仍然对这个问题感兴趣。 That's why I'm sharing how I resolved this problem. 这就是为什么我分享解决问题的方式。

  • I save full image taken from a camera as .jpg. 我将从相机拍摄的完整图像另存为.jpg。
  • Then I get rotation angle from .jpg file. 然后我从.jpg文件获得旋转角度。 The problem of cropping different region was because each image has rotation angle which I was able to get from ExifInterface 裁剪不同区域的问题是因为每个图像都具有我可以从ExifInterface获得的旋转角度
  • Scale crop region(light rectangle on camera) to saved image dimensions. 缩放裁剪区域(相机上的矩形)以保存图像尺寸。
  • Rotate crop region for an angle which has been got previously. 将作物区域旋转一个先前获得的角度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM