无法获取相机预览(调用相机),HTC Thunderbolt

发布于 2025-01-03 21:16:49 字数 11838 浏览 1 评论 0原文

我是 Android 应用程序开发的新手,所以请任何答案都包含代码示例。

我的问题是这样的,我必须开发一个相机应用程序,将 mjpeg 流式传输到服务器。

现在我已经成功地为我的摩托罗拉 Atrix 创建了该应用程序,并且效果非常好。我的问题是,在我签署并导出该应用程序并将其安装在 HTC Thunderbolt 上进行其他设备测试后,我发现当我尝试调用相机时它会冻结并且应用程序崩溃。

从那时起,我尝试了网络上尤其是这些论坛中用于相机预览的每个示例/示例代码,并且我不断得到相同的结果。我让它在我的 Atrix 上工作,但在 Thunderbolt 上不行。

当应用程序在 Thunderbolt 上运行时,我得到了这样的信息:

07-01 16:11:12.643: D/AndroidRuntime(1140): Shutting down VM
07-01 16:11:12.643: W/dalvikvm(1140): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-01 16:11:12.653: E/AndroidRuntime(1140): FATAL EXCEPTION: main
07-01 16:11:12.653: E/AndroidRuntime(1140): java.lang.RuntimeException: Fail to connect to camera service
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.hardware.Camera.native_setup(Native Method)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.hardware.Camera.<init>(Camera.java:258)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.hardware.Camera.open(Camera.java:235)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at vcore.java.MainActivity.surfaceCreated(MainActivity.java:95)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.View.draw(View.java:6883)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.View.draw(View.java:6883)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewRoot.draw(ViewRoot.java:1522)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.os.Looper.loop(Looper.java:123)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at java.lang.reflect.Method.invoke(Method.java:507)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at dalvik.system.NativeStart.main(Native Method)

我给 HTC 发了电子邮件,他们说他们根本无法帮助我,所以我想我会在这里碰碰运气。

有谁知道如何正确调用 HTC Thunderbolt 的相机(用于相机预览)?

如果您能提供代码示例,我将不胜感激。

我尝试过的一些例子是:

https://stackoverflow.com/a/4798958/1195751

HTC Desire HD 不接受 setParameter() hardware.Camera 这也不起作用

Android:HTC EVO 上的相机预览方向(Android 2.1 或 2.2) 这也不是

我真的尝试了一切来解决这个问题,但还没有任何工作,所以拜托,任何解决这个问题的方法都会非常有帮助。

更新 我已经在清单中设置了权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="vcore.java"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission>
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.FLASHLIGHT"
    android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
    android:protectionLevel="normal"/>
    <uses-feature android:name="android.hardware.camera.flash" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"></uses-permission>
    <uses-permission android:name="android.permission.RECORD_VIDEO"></uses-permission> 
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <supports-screens
        android:largeScreens="true"   
        android:normalScreens="true"
        android:smallScreens="true"
        android:anyDensity="true" />

    <application
        android:icon="@drawable/logo"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity"
            android:screenOrientation="portrait" >
            <intent-filter >

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>

        <activity android:name=".PushCamera"
                  android:label="@string/app_name">

            <!--   android:screenOrientation="portrait"  -->
        </activity>
         <activity android:name="ImageDisplayActivity"></activity>
    </application>

</manifest>

更新 2

public void surfaceCreated(SurfaceHolder holder) {
        try {
            if (mCamera != null) {
                try {
                    mCamera.stopPreview();
                } catch (Exception ignore) {
                }
                try {
                    mCamera.release();
                } catch (Exception ignore) {
                }
                mCamera = null;
            }

            mCamera = Camera.open();
            mCamera.setPreviewDisplay(holder);
        } catch (Exception ex) {
            try {
                if (mCamera != null) {
                    try {
                        mCamera.stopPreview();
                    } catch (Exception ignore) {
                    }
                    try {
                        mCamera.release();
                    } catch (Exception ignore) {
                    }
                    mCamera = null;
                }
            } catch (Exception ignore) {

            }
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        try {
            if (mCamera != null) {
                try {
                    mCamera.stopPreview();
                } catch (Exception ignore) {
                }
                try {
                    mCamera.release();
                } catch (Exception ignore) {
                }
                mCamera = null;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    Log.e(TAG, "surfaceChanged");
    try {
        Camera.Parameters parameters = mCamera.getParameters();
        try {
            List<Camera.Size> supportedSizes = null;
            //On older devices (<1.6) the following will fail
            //the camera will work nevertheless
            supportedSizes = parameters.getSupportedPreviewSizes();

            //preview form factor
            float ff = (float)w/h;
            Log.d("Mixare", "Screen res: w:"+ w + " h:" + h + " aspect ratio:" + ff);

            //holder for the best form factor and size
            float bff = 0;
            int bestw = 0;
            int besth = 0;
            Iterator<Camera.Size> itr = supportedSizes.iterator();

            //we look for the best preview size, it has to be the closest to the
            //screen form factor, and be less wide than the screen itself
            //the latter requirement is because the HTC Hero with update 2.1 will
            //report camera preview sizes larger than the screen, and it will fail
            //to initialize the camera
            //other devices could work with previews larger than the screen though
            while(itr.hasNext()) {
                Camera.Size element = itr.next();
                //current form factor
                float cff = (float)element.width/element.height;
                //check if the current element is a candidate to replace the best match so far
                //current form factor should be closer to the bff
                //preview width should be less than screen width
                //preview width should be more than current bestw
                //this combination will ensure that the highest resolution will win
                Log.d("Mixare", "Candidate camera element: w:"+ element.width + " h:" + element.height + " aspect ratio:" + cff);
                if ((ff-cff <= ff-bff) && (element.width <= w) && (element.width >= bestw)) {
                    bff=cff;
                    bestw = element.width;
                    besth = element.height;
                }
            } 
            Log.d("Mixare", "Chosen camera element: w:"+ bestw + " h:" + besth + " aspect ratio:" + bff);
            //Some Samsung phones will end up with bestw and besth = 0 because their minimum preview size is bigger then the screen size.
            //In this case, we use the default values: 480x320
            if ((bestw == 0) || (besth == 0)){
                Log.d("Mixare", "Using default camera parameters!");
                bestw = 480;
                besth = 320;
            }
            parameters.setPreviewSize(bestw, besth);
        } catch (Exception ex) {
            parameters.setPreviewSize(480 , 320);
        }

        mCamera.setParameters(parameters);
        mCamera.startPreview();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

I am new to android app development so please any answers would be great with code examples.

My problem is this, I have to develop a camera app that streams mjpeg to a server.

Now I have successfully created the app for my Motorola Atrix and that works great. My problem is after I signed and exported that app and installed it on an HTC Thunderbolt for additional device testing, I found that it freezes when I try to call for the camera and the app crashes.

Since then I have tried every example/sample code for camera preview on the web and in these forums especially and I constantly get the same results. I get it to work on my Atrix but not on the thunderbolt.

When the app runs on the thunderbolt I get this:

07-01 16:11:12.643: D/AndroidRuntime(1140): Shutting down VM
07-01 16:11:12.643: W/dalvikvm(1140): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-01 16:11:12.653: E/AndroidRuntime(1140): FATAL EXCEPTION: main
07-01 16:11:12.653: E/AndroidRuntime(1140): java.lang.RuntimeException: Fail to connect to camera service
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.hardware.Camera.native_setup(Native Method)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.hardware.Camera.<init>(Camera.java:258)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.hardware.Camera.open(Camera.java:235)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at vcore.java.MainActivity.surfaceCreated(MainActivity.java:95)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.View.draw(View.java:6883)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.View.draw(View.java:6883)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewRoot.draw(ViewRoot.java:1522)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.os.Looper.loop(Looper.java:123)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at java.lang.reflect.Method.invoke(Method.java:507)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-01 16:11:12.653: E/AndroidRuntime(1140):     at dalvik.system.NativeStart.main(Native Method)

I emailed HTC themselves and they said they couldn't help me at all so I figured I would try my luck here.

Does anyone know how to properly call the camera (for a camera preview) for an HTC Thunderbolt?

If you could provide code example I would be eternally grateful.

Just some examples of the things I have tried are:

https://stackoverflow.com/a/4798958/1195751

HTC Desire HD not accepts setParameter() with hardware.Camera This also didn't work

Android: Camera Preview Orientation on HTC EVO (Android 2.1 or 2.2) Neither Did this

I have really tried everything to solve this but haven't gotten anything to work so please, any solution to this problem would be extremely helpful.

UPDATE I already have the permissions set in my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="vcore.java"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission>
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.FLASHLIGHT"
    android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
    android:protectionLevel="normal"/>
    <uses-feature android:name="android.hardware.camera.flash" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"></uses-permission>
    <uses-permission android:name="android.permission.RECORD_VIDEO"></uses-permission> 
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <supports-screens
        android:largeScreens="true"   
        android:normalScreens="true"
        android:smallScreens="true"
        android:anyDensity="true" />

    <application
        android:icon="@drawable/logo"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity"
            android:screenOrientation="portrait" >
            <intent-filter >

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>

        <activity android:name=".PushCamera"
                  android:label="@string/app_name">

            <!--   android:screenOrientation="portrait"  -->
        </activity>
         <activity android:name="ImageDisplayActivity"></activity>
    </application>

</manifest>

UPDATE 2

public void surfaceCreated(SurfaceHolder holder) {
        try {
            if (mCamera != null) {
                try {
                    mCamera.stopPreview();
                } catch (Exception ignore) {
                }
                try {
                    mCamera.release();
                } catch (Exception ignore) {
                }
                mCamera = null;
            }

            mCamera = Camera.open();
            mCamera.setPreviewDisplay(holder);
        } catch (Exception ex) {
            try {
                if (mCamera != null) {
                    try {
                        mCamera.stopPreview();
                    } catch (Exception ignore) {
                    }
                    try {
                        mCamera.release();
                    } catch (Exception ignore) {
                    }
                    mCamera = null;
                }
            } catch (Exception ignore) {

            }
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        try {
            if (mCamera != null) {
                try {
                    mCamera.stopPreview();
                } catch (Exception ignore) {
                }
                try {
                    mCamera.release();
                } catch (Exception ignore) {
                }
                mCamera = null;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    Log.e(TAG, "surfaceChanged");
    try {
        Camera.Parameters parameters = mCamera.getParameters();
        try {
            List<Camera.Size> supportedSizes = null;
            //On older devices (<1.6) the following will fail
            //the camera will work nevertheless
            supportedSizes = parameters.getSupportedPreviewSizes();

            //preview form factor
            float ff = (float)w/h;
            Log.d("Mixare", "Screen res: w:"+ w + " h:" + h + " aspect ratio:" + ff);

            //holder for the best form factor and size
            float bff = 0;
            int bestw = 0;
            int besth = 0;
            Iterator<Camera.Size> itr = supportedSizes.iterator();

            //we look for the best preview size, it has to be the closest to the
            //screen form factor, and be less wide than the screen itself
            //the latter requirement is because the HTC Hero with update 2.1 will
            //report camera preview sizes larger than the screen, and it will fail
            //to initialize the camera
            //other devices could work with previews larger than the screen though
            while(itr.hasNext()) {
                Camera.Size element = itr.next();
                //current form factor
                float cff = (float)element.width/element.height;
                //check if the current element is a candidate to replace the best match so far
                //current form factor should be closer to the bff
                //preview width should be less than screen width
                //preview width should be more than current bestw
                //this combination will ensure that the highest resolution will win
                Log.d("Mixare", "Candidate camera element: w:"+ element.width + " h:" + element.height + " aspect ratio:" + cff);
                if ((ff-cff <= ff-bff) && (element.width <= w) && (element.width >= bestw)) {
                    bff=cff;
                    bestw = element.width;
                    besth = element.height;
                }
            } 
            Log.d("Mixare", "Chosen camera element: w:"+ bestw + " h:" + besth + " aspect ratio:" + bff);
            //Some Samsung phones will end up with bestw and besth = 0 because their minimum preview size is bigger then the screen size.
            //In this case, we use the default values: 480x320
            if ((bestw == 0) || (besth == 0)){
                Log.d("Mixare", "Using default camera parameters!");
                bestw = 480;
                besth = 320;
            }
            parameters.setPreviewSize(bestw, besth);
        } catch (Exception ex) {
            parameters.setPreviewSize(480 , 320);
        }

        mCamera.setParameters(parameters);
        mCamera.startPreview();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一世旳自豪 2025-01-10 21:16:49
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {



    if (isPreviewRunning) {
        camera.stopPreview();
    }
    try{
    Camera.Parameters p = camera.getParameters();
    if(p!=null){
    List<Size> sizes = p.getSupportedPreviewSizes();
    Size optimalSize = getOptimalPreviewSize(sizes, w, h);
    p.setPreviewSize(optimalSize.width, optimalSize.height);
    camera.setParameters(p);

    camera.setPreviewDisplay(holder);;
    camera.startPreview();

    }
    } catch (IOException e) {
        // TODO Auto-generated catch block


        e.printStackTrace();
    }

    isPreviewRunning = true;
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
    // TODO Auto-generated method stub
    final double ASPECT_TOLERANCE = 0.05;
    double targetRatio = (double) w / h;
    if (sizes == null) return null;

    Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    // Try to find an size match aspect ratio and size
    for (Size size : sizes) {
    double ratio = (double) size.width / size.height;
    if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
    if (Math.abs(size.height - targetHeight) < minDiff) {
    optimalSize = size;
    minDiff = Math.abs(size.height - targetHeight);
    }
    }

    // Cannot find the one match the aspect ratio, ignore the requirement
    if (optimalSize == null) {
    minDiff = Double.MAX_VALUE;
    for (Size size : sizes) {
    if (Math.abs(size.height - targetHeight) < minDiff) {
    optimalSize = size;
    minDiff = Math.abs(size.height - targetHeight);
    }
    }
    }
    return optimalSize;
}

将此代码放入 SurfaceChanged() 中。
getOptimalPreviewSize() 用于根据设备分辨率设置预览参数。
所以打开相机预览时不会崩溃。
试试这个。
这对我在不同的设备上都很有效。

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {



    if (isPreviewRunning) {
        camera.stopPreview();
    }
    try{
    Camera.Parameters p = camera.getParameters();
    if(p!=null){
    List<Size> sizes = p.getSupportedPreviewSizes();
    Size optimalSize = getOptimalPreviewSize(sizes, w, h);
    p.setPreviewSize(optimalSize.width, optimalSize.height);
    camera.setParameters(p);

    camera.setPreviewDisplay(holder);;
    camera.startPreview();

    }
    } catch (IOException e) {
        // TODO Auto-generated catch block


        e.printStackTrace();
    }

    isPreviewRunning = true;
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
    // TODO Auto-generated method stub
    final double ASPECT_TOLERANCE = 0.05;
    double targetRatio = (double) w / h;
    if (sizes == null) return null;

    Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    // Try to find an size match aspect ratio and size
    for (Size size : sizes) {
    double ratio = (double) size.width / size.height;
    if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
    if (Math.abs(size.height - targetHeight) < minDiff) {
    optimalSize = size;
    minDiff = Math.abs(size.height - targetHeight);
    }
    }

    // Cannot find the one match the aspect ratio, ignore the requirement
    if (optimalSize == null) {
    minDiff = Double.MAX_VALUE;
    for (Size size : sizes) {
    if (Math.abs(size.height - targetHeight) < minDiff) {
    optimalSize = size;
    minDiff = Math.abs(size.height - targetHeight);
    }
    }
    }
    return optimalSize;
}

Put this code in your surfaceChanged().
getOptimalPreviewSize() is used to set preview parameters according to device resolution.
So it will not crash when camera preview is opened.
Try this.
This works good for me in different devices.

爱格式化 2025-01-10 21:16:49

您需要在 androidmanifest.xml 中授予使用相机的权限。

<uses-permission android:name=”android.permission.CAMERA”/>

You need to give permission to use camera in androidmanifest.xml.

<uses-permission android:name=”android.permission.CAMERA”/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文