我有一个适用于大多数手机的相机应用程序,但我今天收到了用户的错误。该错误表明应用程序在Camera.setParameters()
上崩溃我已阅读This StackOverflow Post有关该主题的内容,但已实施相同的解决方案。
这是我正在使用的代码:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
camera.setDisplayOrientation(90);
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = getBestPreviewSize(w, h);
parameters.setPreviewSize(size.width, size.height); // preview size
camera.setParameters(parameters);
camera.startPreview();
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
// You need to choose the most appropriate previewSize for your app
Camera.Size previewSize = // .... select one of previewSizes here
}
private Camera.Size getBestPreviewSize(int width, int height)
{
// Get For Photo Size
Camera.Parameters camparams = camera.getParameters();
// Find the Best Preview Size
List<Size> sizes = camparams.getSupportedPreviewSizes();
Camera.Size result=null;
int finalHeight = 0;
for (Size s : sizes) {
if (s.width <= width && s.height <= height) {
if (result == null) {
result = s;
finalHeight = s.height;
} else {
int resultArea=result.width*result.height;
int newArea=s.width*s.height;
if (newArea>resultArea) {
result=s;
finalHeight = s.height;
}
}
}
}
// Just in case...
if (result == null) {
finalHeight = height;
}
result.width = (int)(finalHeight*cameraRatio);
return result;
}
我的想法是在try / catch
附近设置camera.setParameter(size.width, size.height)
,但我不知道是否会在失败时使其崩溃?
以下是在developer.android.com上发送给我的崩溃日志:
java.lang.RuntimeException:setParameters失败了 android.hardware.Camera.native_setParameters(Native Method)at android.hardware.Camera.setParameters(Camera.java:953)at net.feltpad.mosaic.Preview.surfaceChanged(CameraPreview.java:145)at at android.view.SurfaceView.updateWindow(SurfaceView.java:557)at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)at android.view.ViewGroup.drawChild(ViewGroup.java:1730)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.ViewGroup.drawChild(ViewGroup.java:1730)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.ViewGroup.drawChild(ViewGroup.java:1730)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.View.draw(View.java:6988)at android.widget.FrameLayout.draw(FrameLayout.java:357)at android.view.ViewGroup.drawChild(ViewGroup.java:1732)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.ViewGroup.drawChild(ViewGroup.java:1730)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.ViewGroup.drawChild(ViewGroup.java:1730)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.ViewGroup.drawChild(ViewGroup.java:1730)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.ViewGroup.drawChild(ViewGroup.java:1730)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.View.draw(View.java:6988)at android.widget.FrameLayout.draw(FrameLayout.java:357)at android.view.ViewGroup.drawChild(ViewGroup.java:1732)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)at android.view.View.draw(View.java:6988)at android.widget.FrameLayout.draw(FrameLayout.java:357)at com.android.internal.policy.impl.PhoneWindow $ DecorView.draw(PhoneWindow.java:1961) 在android.view.ViewRoot.draw(ViewRoot.java:1602)at android.view.ViewRoot.performTraversals(ViewRoot.java:1323)at android.view.ViewRoot.handleMessage(ViewRoot.java:1961)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:150)at android.app.ActivityThread.main(ActivityThread.java:4333)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:507)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)at dalvik.system.NativeStart.main(原生方法)
答案 0 :(得分:1)
此代码假定您可以将result.width
设置为某个动态计算值。这不适用于所有设备。 result
必须是getSupportedPreviewSizes()
返回的值之一才能在所有设备上可靠地工作,在您的情况下,它不一定是其中一个值。
答案 1 :(得分:0)
您可以拥有CameraHost的扩展类。在此类中,您可以覆盖adjustPreviewParameters函数,这会导致错误的结果大小。以下是我对相机预览的修复:
@Override
public Parameters adjustPreviewParameters(Parameters parameters) {
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = sizes.get(0);
parameters.setPreviewSize(cs.width, cs.height);
return super.adjustPreviewParameters(parameters);
}