Android、OpenCv、捕获的照片尺寸与预览不匹配
我有一个JavaCameraView子类,它允许用户拍摄和保存照片。问题是,我通过onCameraFrame()接收的预览帧与捕获和保存的照片的尺寸不相同,导致保存的图像相对于帧显示为裁剪。输入帧为1080*1440(3:4),捕获帧为2988*5312(9:16) 我的代码如下:Android、OpenCv、捕获的照片尺寸与预览不匹配,android,opencv,android-camera,Android,Opencv,Android Camera,我有一个JavaCameraView子类,它允许用户拍摄和保存照片。问题是,我通过onCameraFrame()接收的预览帧与捕获和保存的照片的尺寸不相同,导致保存的图像相对于帧显示为裁剪。输入帧为1080*1440(3:4),捕获帧为2988*5312(9:16) 我的代码如下: public void takePicture() { Log.i(CaptureActivity.TAG, "MyCameraView, takePicture()"); // Postview a
public void takePicture() {
Log.i(CaptureActivity.TAG, "MyCameraView, takePicture()");
// Postview and jpeg are sent in the same buffers if the queue is not empty when performing a capture.
// Clear up buffers to avoid mCamera.takePicture to be stuck because of a memory issue
mCamera.setPreviewCallback(null);
// PictureTakenListener is implemented by the current class
mCamera.takePicture(null, null, this);
}
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
// The camera preview was automatically stopped. Start it again.
mCamera.startPreview();
mCamera.setPreviewCallback(this);
Mat mat = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Log.d(CaptureActivity.TAG, mat.toString());
//an interface method to be implemented by CaptureActivity
if (mPictureTakenListener != null)
mPictureTakenListener.receivePicture(mat);
}
在另一节课上,我收到图片并保存
@Override
public void receivePicture(Mat mat) {
File path = new File(Environment.getExternalStorageDirectory() + "/myCamera/");
path.mkdirs();
File file = new File(path, System.currentTimeMillis() + "recieved" + ".png");
String filename = file.toString();
Log.d(CaptureActivity.TAG, "was imwrite a success? "
+ Imgcodecs.imwrite(filename, mat));
}
此类还实现了CameraBridgeViewBase.CvCameraViewListener2
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
Log.d(TAG, "frame info " + frame.toString());
return frame;
}
您应该使用和来配置从预览和拍照中获得的内容。请注意,您不一定会发现两者的尺寸完全相同(通常可供拍摄的尺寸可能高于预览尺寸),因此请仔细查看并找出最适合您的任务的尺寸。您是对的。不建议在预览和捕获时使用相同的纵横比。因此,在您的子类中,您应该重写
initializeCamera()
方法,如下所示:
@Override
protected void AllocateCache() {
super.AllocateCache();
setPictureSize();
}
protected boolean setPictureSize() {
try {
Camera.Parameters params = mCamera.getParameters();
Log.d(TAG, "getSupportedPictureSizes()");
List<Camera.Size> sizes = params.getSupportedPictureSizes();
if (sizes == null) {
Log.w(TAG, "getSupportedPictureSizes() = null, cannot set a custom size");
return false;
}
int maxSize = 0;
// choose the largest size that matches the preview AR
for (android.hardware.Camera.Size size : sizes) {
if (size.height * mFrameWidth != size.width * mFrameHeight) {
continue; // the picture size doesn't match
}
if (maxSize > size.width * size.height) {
continue; // don't need this size
}
params.setPictureSize(size.width, size.height);
maxSize = size.width * size.height;
}
if (maxSize == 0) {
Log.w(TAG, "getSupportedPictureSizes() has no matches for " + mFrameWidth + 'x' + mFrameHeight);
return false;
}
Log.d(TAG, "try Picture size " + params.getPictureSize().width + 'x' + params.getPictureSize().height);
mCamera.setParameters(params);
} catch (Exception e) {
Log.e(TAG, "setPictureSize for " + mFrameWidth + 'x' + mFrameHeight, e);
return false;
}
return true;
}
@覆盖
受保护的void分配器切(){
super.AllocateCache();
setPictureSize();
}
受保护的布尔值setPictureSize(){
试一试{
Camera.Parameters params=mCamera.getParameters();
d(标记“getSupportedPictureSizes()”);
列表大小=params.getSupportedPictureSizes();
如果(大小==null){
w(标记“getSupportedPictureSizes()=null,无法设置自定义大小”);
返回false;
}
int maxSize=0;
//选择与预览AR匹配的最大大小
用于(android.hardware.Camera.Size:尺寸){
if(size.height*mFrameWidth!=size.width*mFrameHeight){
continue;//图片大小不匹配
}
如果(maxSize>size.width*size.height){
continue;//不需要这个大小
}
参数setPictureSize(大小、宽度、大小、高度);
maxSize=size.width*size.height;
}
如果(maxSize==0){
Log.w(标记“getSupportedPictureSizes()没有与“+mFrameWidth+'x'+mFrameHeight”匹配的项);
返回false;
}
Log.d(标记“try Picture size”+params.getPictureSize().width+'x'+params.getPictureSize().height);
mCamera.setParameters(参数);
}捕获(例外e){
Log.e(标记为“+mFrameWidth+'x'+mFrameHeight,e”的setPictureSize);
返回false;
}
返回true;
}
}
如您所见,我实际上重写了方法AllocateCache()
,因为OpenCV在打开相机设备和开始预览之间没有提供更好的挂钩。在预览处于活动状态时更改相机参数(甚至图片大小)可能会在某些设备上失败
请注意,上面的代码甚至没有尝试设置不完全适合所选预览的图片大小。对于99.9%的情况,这是可以的,但是如果一个奇怪的相机没有与OpenCV为您的布局选择的预览大小相匹配的图片大小,您有两个选择:或者寻找另一个预览大小,或者尝试一些“足够近”的图片大小