Android开发相机放大缩小的(关键代码)

释放双眼,带上耳机,听听看~!

相机放大缩小的关键代码:

 

private float oldDist = 1f;

 

 

 @Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() == 1) {
handleFocusMetering(event, camera);
} else {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = getFingerSpacing(event);
break;
case MotionEvent.ACTION_MOVE:
float newDist = getFingerSpacing(event);
if (newDist > oldDist) {
Log.e("Camera","进入放大手势");
handleZoom(true, camera);
} else if (newDist < oldDist) {
Log.e("Camera","进入缩小手势");
handleZoom(false, camera);
}
oldDist = newDist;
break;
}
}
return true;
}
private void handleZoom(boolean isZoomIn, Camera camera) {
Log.e("Camera","进入缩小放大方法");
Camera.Parameters params = camera.getParameters();
if (params.isZoomSupported()) {
int maxZoom = params.getMaxZoom();
int zoom = params.getZoom();
if (isZoomIn && zoom < maxZoom) {
Log.e("Camera","进入放大方法zoom="+zoom);
zoom++;
} else if (zoom > 0) {
Log.e("Camera","进入缩小方法zoom="+zoom);
zoom--;
}
params.setZoom(zoom);
camera.setParameters(params);
} else {
Log.i(TAG, "zoom not supported");
}
}
private static void handleFocusMetering(MotionEvent event, Camera camera) {
Log.e("Camera","进入handleFocusMetering");
Camera.Parameters params = camera.getParameters();
Camera.Size previewSize = params.getPreviewSize();
Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f, previewSize);
Rect meteringRect = calculateTapArea(event.getX(), event.getY(), 1.5f, previewSize);
camera.cancelAutoFocus();
if (params.getMaxNumFocusAreas() > 0) {
List<Camera.Area> focusAreas = new ArrayList<>();
focusAreas.add(new Camera.Area(focusRect, 800));
params.setFocusAreas(focusAreas);
} else {
Log.i(TAG, "focus areas not supported");
}
if (params.getMaxNumMeteringAreas() > 0) {
List<Camera.Area> meteringAreas = new ArrayList<>();
meteringAreas.add(new Camera.Area(meteringRect, 800));
params.setMeteringAreas(meteringAreas);
} else {
Log.i(TAG, "metering areas not supported");
}
final String currentFocusMode = params.getFocusMode();
params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
camera.setParameters(params);
camera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
Camera.Parameters params = camera.getParameters();
params.setFocusMode(currentFocusMode);
camera.setParameters(params);
}
});
}
private static float getFingerSpacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
Log.e("Camera","getFingerSpacing ,计算距离 = " + (float) Math.sqrt(x * x + y * y));
return (float) Math.sqrt(x * x + y * y);
}
private static Rect calculateTapArea(float x, float y, float coefficient, Camera.Size previewSize) {
float focusAreaSize = 300;
int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue();
int centerX = (int) (x / previewSize.width - 1000);
int centerY = (int) (y / previewSize.height - 1000);
int left = clamp(centerX - areaSize / 2, -1000, 1000);
int top = clamp(centerY - areaSize / 2, -1000, 1000);
RectF rectF = new RectF(left, top, left + areaSize, top + areaSize);
return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right), Math.round(rectF.bottom));
}
private static int clamp(int x, int min, int max) {
if (x > max) {
return max;
}
if (x < min) {
return min;
}
return x;
}

 

 

点击打开源码链接

人已赞赏
Android文章

Android下拉列表弹出视图

2020-1-19 9:59:54

Android文章

Android开发仿IOS的滑动按钮

2020-1-19 11:00:46

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索