我正尝试在相机预览模式下使用双指触摸手势进行缩放。但我无法做到这一点。我在ImageView上完成了Zoom控制,它工作正常。现在我想在相机预览模式下这样做,即当我们启动相机时,我们可以使用双指手势进行缩放。
下面我添加了用于缩放图像的双指手势代码。
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
ImageView view = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// for draging the image
matrix.set(savedMatrix); matrix.postTranslate(event.getX() -
start.x, event.getY() - start.y);
break;
} else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
return true;
}
请指导我如何在相机预览中执行此手势变焦控制。 至少提供教程链接。
谢谢 Vikash
答案 0 :(得分:9)
这是我在github上找到的解决方案: https://github.com/Betulaphobe/ChatCapsule/blob/8d4f00a7d4c9166aa7ce974670fdf3033a6064f4/chc_application/src/chc/helpers/CameraPreview.java
@Override
public boolean onTouchEvent(MotionEvent event) {
// Get the pointer ID
Camera.Parameters params = mCamera.getParameters();
int action = event.getAction();
if (event.getPointerCount() > 1) {
// handle multi-touch events
if (action == MotionEvent.ACTION_POINTER_DOWN) {
mDist = getFingerSpacing(event);
} else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) {
mCamera.cancelAutoFocus();
handleZoom(event, params);
}
} else {
// handle single touch events
if (action == MotionEvent.ACTION_UP) {
handleFocus(event, params);
}
}
return true;
}
private void handleZoom(MotionEvent event, Camera.Parameters params) {
int maxZoom = params.getMaxZoom();
int zoom = params.getZoom();
float newDist = getFingerSpacing(event);
if (newDist > mDist) {
//zoom in
if (zoom < maxZoom)
zoom++;
} else if (newDist < mDist) {
//zoom out
if (zoom > 0)
zoom--;
}
mDist = newDist;
params.setZoom(zoom);
mCamera.setParameters(params);
}
public void handleFocus(MotionEvent event, Camera.Parameters params) {
int pointerId = event.getPointerId(0);
int pointerIndex = event.findPointerIndex(pointerId);
// Get the pointer's current position
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);
List<String> supportedFocusModes = params.getSupportedFocusModes();
if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean b, Camera camera) {
// currently set to auto-focus on single touch
}
});
}
}
/** Determine the space between the first two fingers */
private float getFingerSpacing(MotionEvent event) {
// ...
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
答案 1 :(得分:3)
我得到了问题的解决方案,以下是解决方案。
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM) {
float newDist = spacing(event);
double zoomDist = newDist-oldDist;
if(zoomDist > 0){
if(zoomDist > 50 && zoomDist <= 200){
if (curZoomLevel < mZoomMax && gestureZoom == 0) {
gestureZoom ++;
GestureZoomIn();
}
}else if(zoomDist > 200 && zoomDist <= 300){
if (curZoomLevel < mZoomMax && gestureZoom == 1) {
gestureZoom ++;
GestureZoomIn();
}
}else if(zoomDist > 300 && zoomDist <= 400){
if (curZoomLevel < mZoomMax && gestureZoom == 2) {
gestureZoom++;
GestureZoomIn();
}
//
private void GestureZoomIn(){
if (mParameters.isZoomSupported()){
mZoomMax = mParameters.getMaxZoom();
if (zoom_text_value<mZoomMax) {
zoom_text_value++;
curZoomLevel++;
zoom_float=zoom_float+0.5;
onZoomValueChanged(curZoomLevel);
//
private void GestureZoomOut(){
if (mParameters.isZoomSupported()){
if (0<zoom_text_value) {
zoom_text_value--;
curZoomLevel--;
zoom_float=zoom_float-0.5;
onZoomValueChanged(curZoomLevel);
以上代码我用来实现相机评测中的多变焦。
答案 2 :(得分:2)
看看我的实施情况。支持相机API:相机和相机2。您可以使用双指触摸进行缩放。
这就是我在活动中使用代码的方式。
仅适用于相机API:
private void setupZoomHandler(final Camera.Parameters parameters) {
if ( parameters.isZoomSupported() ) {
SimpleZoomHandlerBuilder.forView( mCameraPreview )
.setMaxZoom( parameters.getMaxZoom() )
.setZoomListener( new SimpleZoomHandler.IZoomHandlerListener() {
@Override
public void onZoomChanged(int newZoom) {
Camera.Parameters params = mCamera.getParameters();
params.setZoom( newZoom );
mCamera.setParameters( params );
}
} )
.build();
}
}
对于camera2 API而言:
private void setupZoomHandler(CameraCharacteristics cameraCharacteristics) {
ActiveArrayZoomHandlerBuilder.forView( mTextureView )
.setActiveArraySize( cameraCharacteristics.get( CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE ) )
.setMaxZoom( cameraCharacteristics.get( CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM ) * 10 )
.setZoomListener( new ActiveArrayZoomHandler.IZoomHandlerListener() {
@Override
public void onZoomChanged(Rect zoom) {
mCaptureRequestBuilder.set( CaptureRequest.SCALER_CROP_REGION, zoom );
updatePreview();
}
} )
.build();
}
以下是缩放处理所需的类:
public abstract class AbstractZoomHandler implements View.OnTouchListener {
private static final int FINGER_SPACING_DELTA_FOR_ZOOM = 25;
private static final int FINGER_SPACING_ZOOM_INCREMENT = 5;
private static final float DEFAULT_ZOOM_HARDNESS = 0.4f;
private float lastFingerSpacingTime;
private float fingerSpacingBuffer;
protected int zoomLevel;
protected float zoomIncrement;
protected float maxZoom;
public AbstractZoomHandler(View touchableView) {
touchableView.setOnTouchListener( this );
this.lastFingerSpacingTime = 0;
this.fingerSpacingBuffer = 0;
this.zoomLevel = 1;
setZoomHardness( DEFAULT_ZOOM_HARDNESS );
}
public void setMaxZoom(float maxZoom) {
this.maxZoom = maxZoom;
}
public void setZoomHardness(float zoomHardness) {
this.zoomIncrement = Math.round( zoomHardness * FINGER_SPACING_ZOOM_INCREMENT );
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if ( !isPrepared() ) {
return true;
}
if ( event.getAction() == MotionEvent.ACTION_UP ) {
fingerSpacingBuffer = 0;
return true;
}
if ( isTwoFingersTouchEvent( event ) ) {
int newZoomLevel = performTwoFingersZoom( event );
recalculateZoom( newZoomLevel );
}
return true;
}
private boolean isTwoFingersTouchEvent(MotionEvent event) {
return event.getPointerCount() == 2;
}
private int performTwoFingersZoom(MotionEvent event) {
int newZoomLevel = zoomLevel;
float currentFingerSpacingTime = getFingerSpacing( event );
fingerSpacingBuffer += currentFingerSpacingTime - lastFingerSpacingTime;
if ( fingerSpacingBuffer >= FINGER_SPACING_DELTA_FOR_ZOOM && maxZoom > zoomLevel ) {
newZoomLevel += zoomIncrement;
fingerSpacingBuffer = 0;
} else if ( fingerSpacingBuffer <= -FINGER_SPACING_DELTA_FOR_ZOOM && zoomLevel > 1 ) {
newZoomLevel -= zoomIncrement;
;
fingerSpacingBuffer = 0;
}
lastFingerSpacingTime = currentFingerSpacingTime;
return newZoomLevel;
}
private void recalculateZoom(int newZoomLevel) {
if ( newZoomLevel == zoomLevel ) {
return;
}
zoomLevel = newZoomLevel;
notifyZoomChanged( zoomLevel );
}
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 abstract void notifyZoomChanged(int zoom);
public abstract boolean isPrepared();
}
对于相机:
public class SimpleZoomHandler extends AbstractZoomHandler {
private IZoomHandlerListener zoomHandlerListener;
SimpleZoomHandler(View touchableView) {
super( touchableView );
}
public void setZoomHandlerListener(IZoomHandlerListener zoomHandlerListener) {
this.zoomHandlerListener = zoomHandlerListener;
}
@Override
public void notifyZoomChanged(int zoom) {
zoomHandlerListener.onZoomChanged( zoom );
}
@Override
public boolean isPrepared() {
return zoomHandlerListener != null;
}
public interface IZoomHandlerListener {
void onZoomChanged(int newZoom);
}
}
对于Camera2:
public class ActiveArrayZoomHandler extends AbstractZoomHandler {
private IZoomHandlerListener zoomHandlerListener;
private Rect activeArraySize;
ActiveArrayZoomHandler(View touchableView) {
super( touchableView );
}
public void setZoomHandlerListener(IZoomHandlerListener zoomHandlerListener) {
this.zoomHandlerListener = zoomHandlerListener;
}
public void setActiveArraySize(Rect activeArraySize) {
this.activeArraySize = activeArraySize;
}
@Override
public void notifyZoomChanged(int zoom) {
int minW = (int) (activeArraySize.width() / maxZoom);
int minH = (int) (activeArraySize.height() / maxZoom);
int difW = activeArraySize.width() - minW;
int difH = activeArraySize.height() - minH;
int cropW = difW / 100 * zoomLevel;
int cropH = difH / 100 * zoomLevel;
cropW -= cropW & 3;
cropH -= cropH & 3;
Rect zoomRect = new Rect( cropW, cropH, activeArraySize.width() - cropW, activeArraySize.height() - cropH );
zoomHandlerListener.onZoomChanged( zoomRect );
}
@Override
public boolean isPrepared() {
return zoomHandlerListener != null && activeArraySize != null;
}
public interface IZoomHandlerListener {
void onZoomChanged(Rect zoom);
}
}
和他们各自的Builder使其界面流畅。对于相机API:
public class SimpleZoomHandlerBuilder {
private SimpleZoomHandler simpleZoomHandler;
public static SimpleZoomHandlerBuilder forView(View touchableView) {
return new SimpleZoomHandlerBuilder( touchableView );
}
private SimpleZoomHandlerBuilder(View touchableView) {
simpleZoomHandler = new SimpleZoomHandler( touchableView );
}
public SimpleZoomHandlerBuilder setZoomListener(SimpleZoomHandler.IZoomHandlerListener listener) {
simpleZoomHandler.setZoomHandlerListener( listener );
return this;
}
public SimpleZoomHandlerBuilder setMaxZoom(float maxZoom) {
simpleZoomHandler.setMaxZoom( maxZoom );
return this;
}
public SimpleZoomHandler build() {
return simpleZoomHandler;
}
}
对于camera2 API:
public class ActiveArrayZoomHandlerBuilder {
private ActiveArrayZoomHandler activeArrayZoomHandler;
public static ActiveArrayZoomHandlerBuilder forView(View touchableView) {
return new ActiveArrayZoomHandlerBuilder( touchableView );
}
private ActiveArrayZoomHandlerBuilder(View touchableView) {
activeArrayZoomHandler = new ActiveArrayZoomHandler( touchableView );
}
public ActiveArrayZoomHandlerBuilder setZoomListener(ActiveArrayZoomHandler.IZoomHandlerListener listener) {
activeArrayZoomHandler.setZoomHandlerListener( listener );
return this;
}
public ActiveArrayZoomHandlerBuilder setMaxZoom(float maxZoom) {
activeArrayZoomHandler.setMaxZoom( maxZoom );
return this;
}
public ActiveArrayZoomHandlerBuilder setActiveArraySize(Rect activeArraySize) {
activeArrayZoomHandler.setActiveArraySize( activeArraySize );
return this;
}
public ActiveArrayZoomHandler build() {
return activeArrayZoomHandler;
}
}