以下代码在 Galaxy Nexus、Samsung S3 和 HTC One X 上工作正常,但在 Samsung S3 Mini 上崩溃了。
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d(TAG, "entering surfaceChanged");
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
// set barcode scene mode
if (parameters.getSupportedSceneModes() != null && parameters.getSupportedSceneModes().contains(Camera.Parameters.SCENE_MODE_BARCODE)){
parameters.setSceneMode(Camera.Parameters.SCENE_MODE_BARCODE);
Log.d(TAG, "setting parameters.setSceneMode(Camera.Parameters.SCENE_MODE_BARCODE)");
}
Camera.Size size = previewSizes.get(0);
if (previewSizes.size() > 2)
size = previewSizes.get(2);
else if (previewSizes.size() > 1)
size = previewSizes.get(1);
parameters.setPreviewSize(size.width, size.height);
Log.d(TAG, "picsize w: " + Integer.toString(size.width) + " h: " + Integer.toString(size.height));
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR2) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
Log.d(TAG, "setting .setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)");
} else {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
Log.d(TAG, "setting .setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)");
}
// setup cam flash mode
if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FLASH_MODE_AUTO)){
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
Log.d(TAG, "setting .setFlashMode(Camera.Parameters.FLASH_MODE_AUTO)");
}
int maxZoom = parameters.getMaxZoom();
int zoom = (int) Math.floor(maxZoom / 3);
if (parameters.isZoomSupported()) {
if (zoom >= 0 && zoom < maxZoom) {
parameters.setZoom(zoom);
Log.d(TAG, "setting .setZoom(" + Integer.toString(zoom) +")");
} else {
// zoom parameter is incorrect
Log.d(TAG, "setting no zoom");
}
}
camera.setParameters(parameters);
camera.startPreview();
}
日志输出:
8 05-13 10:54:45.864 D/CameraSurfaceView(11784): entering surfaceChanged
9 05-13 10:54:45.874 D/CameraSurfaceView(11784): picsize w: 800 h: 480
10 05-13 10:54:45.874 D/CameraSurfaceView(11784): setting .setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)
11 05-13 10:54:45.874 D/CameraSurfaceView(11784): setting .setFlashMode(Camera.Parameters.FLASH_MODE_AUTO)
12 05-13 10:54:45.874 D/CameraSurfaceView(11784): setting .setZoom(10)
13 05-13 10:54:45.914 D/AndroidRuntime(11784): Shutting down VM
14 05-13 10:54:45.914 W/dalvikvm(11784): threadid=1: thread exiting with uncaught exception (group=0x411422a0)
堆栈跟踪:
java.lang.RuntimeException: setParameters failed
at android.hardware.Camera.native_setParameters(Native Method)
at android.hardware.Camera.setParameters(Camera.java:1452)
at com.getznap.znap.CameraSurfaceView.surfaceChanged(CameraSurfaceView.java:103)
at android.view.SurfaceView.updateWindow(SurfaceView.java:602)
at android.view.SurfaceView.access$000(SurfaceView.java:81)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:173)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:707)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1944)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1113)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
我猜S3 mini在我的一个相机参数上存在问题。由于我没有S3 Mini,所以无法逐个测试所有参数。我的代码中是否有明显的错误?