我在opencv人脸检测示例中实现JavaCamera2View。 该应用程序能够执行捏动作并识别并计算Rect的大小。但是,屏幕根本无法放大或缩小。什么也没发生。 因此我创建了实现JavaCamera2View的新类,以便我有更多空间来编写自己的代码。
所以在我的FdActivity中,我实现了OnTouchListener:
private zoomcameraview mOpenCvCameraView;
.
.
mOpenCvCameraView.enableView();
mOpenCvCameraView.setOnTouchListener(FdActivity.this);
.
.
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return mOpenCvCameraView.setTouch(view,motionEvent, String.valueOf(cameraId));
}
我在zoomcameraview内实现JavaCamera2View:
private float getFingerSpacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
public boolean setTouch(View view, MotionEvent motionEvent, String camId) {
mPreviewRequestBuilder = getmPreviewRequestBuilder();
mCaptureSession = getmCaptureSession();
Log.d("run on touch iside", "onTouch: motion count " +motionEvent.getPointerCount());
try {
CameraManager manager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics characteristics = manager.getCameraCharacteristics(camId);
float maxzoom = (characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM))*10;
Rect m = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
int action = motionEvent.getAction();
float current_finger_spacing;
if (motionEvent.getPointerCount() > 1) {
// Multi touch logic
current_finger_spacing = getFingerSpacing(motionEvent);
Log.d("finger", "setTouch: " + finger_spacing);
if(finger_spacing != 0){
if(current_finger_spacing > finger_spacing && maxzoom > zoom_level){
zoom_level++;
} else if (current_finger_spacing < finger_spacing && zoom_level > 1){
zoom_level--;
}
Log.d("zoom level", "setTouch: " + zoom_level);
int minW = (int) (m.width() / maxzoom);
int minH = (int) (m.height() / maxzoom);
int difW = m.width() - minW;
int difH = m.height() - minH;
int cropW = difW /100 *(int)zoom_level;
int cropH = difH /100 *(int)zoom_level;
cropW -= cropW & 3;
cropH -= cropH & 3;
Rect zoom = new Rect(cropW, cropH, m.width() - cropW, m.height() - cropH);
mPreviewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);
}
finger_spacing = current_finger_spacing;
} else{
return true;
// if (action == MotionEvent.ACTION_UP) {
// return true;
// //single touch logic
// }
}
try {
mCaptureSession
.setRepeatingRequest(mPreviewRequestBuilder.build(), null, getmBackgroundHandler());
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
} catch (CameraAccessException e) {
throw new RuntimeException("can not access camera.", e);
}
return true;
}
在JavaCamera2View中,我设置了getter和setter
public CaptureRequest.Builder getmPreviewRequestBuilder() {
return mPreviewRequestBuilder;
}
public void setmPreviewRequestBuilder(CaptureRequest.Builder mPreviewRequestBuilder) {
this.mPreviewRequestBuilder = mPreviewRequestBuilder;
}
public CameraCaptureSession getmCaptureSession() {
return mCaptureSession;
}
public void setmCaptureSession(CameraCaptureSession mCaptureSession) {
this.mCaptureSession = mCaptureSession;
}
public CameraDevice.StateCallback getmStateCallback() {
return mStateCallback;
}
public Handler getmBackgroundHandler() {
return mBackgroundHandler;
}
public void setmBackgroundHandler(Handler mBackgroundHandler) {
this.mBackgroundHandler = mBackgroundHandler;
}
我不确定出什么问题,但是缩放没有实现,但没有错误。
答案 0 :(得分:0)
很抱歉,经过数天的尝试,我终于解决了。 实施
public boolean setTouch(View view, MotionEvent motionEvent, String camId) {
mPreviewRequestBuilder = getmPreviewRequestBuilder();
mCaptureSession = getmCaptureSession();
Log.d("run on touch iside", "onTouch: motion count " +motionEvent.getPointerCount());
try {
CameraManager manager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics characteristics = manager.getCameraCharacteristics(camId);
float maxzoom = (characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM))*10;
Rect m = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
int action = motionEvent.getAction();
float current_finger_spacing;
if (motionEvent.getPointerCount() > 1) {
// Multi touch logic
current_finger_spacing = getFingerSpacing(motionEvent);
Log.d("finger", "setTouch: " + finger_spacing);
if(finger_spacing != 0){
if(current_finger_spacing > finger_spacing && maxzoom > zoom_level){
zoom_level++;
} else if (current_finger_spacing < finger_spacing && zoom_level > 1){
zoom_level--;
}
Log.d("zoom level", "setTouch: " + zoom_level);
int minW = (int) (m.width() / maxzoom);
int minH = (int) (m.height() / maxzoom);
int difW = m.width() - minW;
int difH = m.height() - minH;
int cropW = difW /100 *(int)zoom_level;
int cropH = difH /100 *(int)zoom_level;
cropW -= cropW & 3;
cropH -= cropH & 3;
Rect zoom = new Rect(cropW, cropH, m.width() - cropW, m.height() - cropH);
mPreviewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);
}
finger_spacing = current_finger_spacing;
} else{
return true;
// if (action == MotionEvent.ACTION_UP) {
// return true;
// //single touch logic
// }
}
try {
mCaptureSession
.setRepeatingRequest(mPreviewRequestBuilder.build(), null, getmBackgroundHandler());
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
} catch (CameraAccessException e) {
throw new RuntimeException("can not access camera.", e);
}
return true;
}
直接在FdAcitvity上解决了我的问题。 现在正确实现了带camera2和pich缩放功能的opencv android
谢谢