无法获得相机预览(通话摄像头),HTC Thunderbolt

时间:2012-02-08 16:24:50

标签: android android-intent android-camera android-camera-intent htc-thunderbolt

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

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

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

从那以后,我尝试了网上和这些论坛中的相机预览的每个示例/示例代码,我不断得到相同的结果。我让它在我的Atrix上工作但不在雷电上工作。

当应用程序在霹雳上运行时,我得到了这个:

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 not accepts setParameter() with hardware.Camera这也无效

Android: Camera Preview Orientation on HTC EVO (Android 2.1 or 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();
    }

}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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()用于根据设备分辨率设置预览参数。 因此,打开相机预览时不会崩溃。 试试这个。 这对我在不同的设备中有用。