zhantong / androidcamera-touchtofocusmeteringzoom Goto Github PK
View Code? Open in Web Editor NEWAndroid相机开发(五): 触摸对焦,触摸测光,二指手势缩放
Android相机开发(五): 触摸对焦,触摸测光,二指手势缩放
Process: com.polarxiong.camerademo, PID: 5430
java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
at com.polarxiong.camerademo.CameraPreview.stopRecording(CameraPreview.java:84)
at com.polarxiong.camerademo.MainActivity$3.onClick(MainActivity.java:43)
at android.view.View.performClick(View.java:6603)
at android.view.View.performClickInternal(View.java:6576)
at android.view.View.access$3100(View.java:780)
at android.view.View$PerformClick.run(View.java:26090)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6702)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
stop之前设置了setOnErrorListener为null,也避免不了崩溃,请问有其他方法吗?
导入Android studio
应该在onPause()中加入preview.removeAllViews();
注意把initCamera()中的FrameLayout preview 换成全局变量
后置摄像头聚焦位置与实际点击位置不符合,需要做更进一步的坐标系变换
我在此代码基础上做了一定改动,有需要的同学可以自取。
仅三个函数有改动
private void handleFocusMetering(MotionEvent event, Camera camera) {
int viewWidth = getWidth();
int viewHeight = getHeight();
Camera.Parameters parameters = camera.getParameters();
android.hardware.Camera.CameraInfo camInfo =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, camInfo);
int displayOrientation = camInfo.orientation;
boolean mirror = (camInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ? true : false); //
Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f, viewWidth, viewHeight, mirror, displayOrientation);
Rect meteringRect = calculateTapArea(event.getX(), event.getY(), 2.0f, viewWidth, viewHeight, mirror, displayOrientation);
Log.i(TAG, String.format("handleFocusMetering: mirror=%s,displayOrientation=%s,focusRect=%s", mirror, displayOrientation, focusRect.toString()));
camera.cancelAutoFocus();
Camera.Parameters params = camera.getParameters();
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 Rect calculateTapArea(float x, float y, float coefficient, int width, int height, boolean mirror, int displayOrientation) {
float focusAreaSize = 300;
int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue();
int centerX = (int) (x / width * 2000 - 1000);
int centerY = (int) (y / height * 2000 - 1000);
int halfAreaSize = areaSize / 2;
RectF rectF = new RectF(clamp(centerX - halfAreaSize, -1000, 1000)
, clamp(centerY - halfAreaSize, -1000, 1000)
, clamp(centerX + halfAreaSize, -1000, 1000)
, clamp(centerY + halfAreaSize, -1000, 1000));
Matrix matrix = new Matrix();
prepareMatrix(matrix, mirror, displayOrientation);
matrix.mapRect(rectF);
return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right), Math.round(rectF.bottom));
}
public static void prepareMatrix(Matrix matrix, boolean mirror, int displayOrientation) {
if (matrix == null) {
return;
}
Matrix _matrix = new Matrix();
matrix.reset();
// Need mirror for front camera.
_matrix.setScale(mirror ? -1 : 1, 1);
// This is the value for android.hardware.Camera.setDisplayOrientation.
_matrix.postRotate(displayOrientation);
_matrix.invert(matrix);//求逆矩阵
}
private static int clamp(int x, int min, int max) {
if (x > max) {
return max;
}
if (x < min) {
return min;
}
return x;
}
参考 谷歌官方相机https://android.googlesource.com/platform/packages/apps/Camera 项目中的FocusOverlayManager
缩放的时候,为什么拍摄的界面抖动比较厉害,不太稳定
建议吧工程代码里面的代理删掉,不然同步下来后每个人都要手动去删掉,否则当本机没有对应gradle版本将导致工程构建不了
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.