gpt4 book ai didi

Android 2.3.4 在 Camera.setParameters 上崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:07 25 4
gpt4 key购买 nike

我有一个适用于大多数手机的相机应用程序,但今天我收到用户的错误消息。该错误表明应用程序在 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;

}

我的想法是在 camera.setParameter(size.width, size.height) 周围放置一个 try/catch 但我不知道这是否会保留失败时崩溃?

这是在 developer.android.com 上发送给我的崩溃日志:

java.lang.RuntimeException: setParameters failed at 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 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) at 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) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method)

最佳答案

此代码假定您可以将 result.width 设置为某个动态计算的值。这不适用于所有设备。 result 必须getSupportedPreviewSizes() 返回的值之一才能在所有设备上可靠地工作,在您的情况下,它不一定这些值之一。

关于Android 2.3.4 在 Camera.setParameters 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13096525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com