开发者

Problem with Camera.startPreview()

I'm fol开发者_如何学JAVAlowing this tutorial to learn Android's Camera API. I made it the end of the first section (right before "Providing an overlay" begins), and I'm getting the following error:

06-20 23:33:50.903: ERROR/AndroidRuntime(1114):     at android.hardware.Camera.startPreview(Native Method)
06-20 23:33:50.903: ERROR/AndroidRuntime(1114):     at com.sobel.Sobel.startCamera(Sobel.java:73)
06-20 23:33:50.903: ERROR/AndroidRuntime(1114):     at com.sobel.Sobel.surfaceChanged(Sobel.java:36)

(Full trace)

Git repo here. Main Activity here. Manifest here.

I checked and re-checked my code and have followed the tutorial to a t, so what could be causing this error?


Try setting the type of Surface in initCamera().

private void initCamera() {
mCamSV = (SurfaceView)findViewById(R.id.surface_camera);
mCamSH = mCamSV.getHolder();
mCamSH.addCallback(this);
**mCamSH.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);**

}

EDIT 1

I am copying all the files here which worked for me with android 2.2 sdk

Activity

package com.stack.camera;


import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.FrameLayout;

public class CameraStackActivity extends Activity implements SurfaceHolder.Callback {
    private Camera mCam;
    private SurfaceView mCamSV;
    private SurfaceHolder mCamSH;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);
    initCamera();
}

@Override
public void onDestroy() {
    stopCamera();
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

    startCamera(holder, width, height);
}

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mCam = Camera.open();
    try {
        mCam.setPreviewDisplay(holder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

private void initCamera() {
    mCamSV = (SurfaceView)findViewById(R.id.surface_camera);
    mCamSH = mCamSV.getHolder();
    mCamSH.addCallback(this);
    mCamSH.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}


private void startCamera(SurfaceHolder sh, int width, int height) {
    Camera.Parameters p = mCam.getParameters();
    // Camera.Size s = p.getSupportedPreviewSizes().get(0);
    p.setPreviewSize(width, height);

    mCam.setParameters(p);

    try {
        mCam.setPreviewDisplay(sh);
    } catch (Exception e) {
    }

    mCam.startPreview();
}

private void stopCamera() {
    mCamSH.removeCallback(this);

    mCam.stopPreview();
    mCam.release();
}

}

Layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <SurfaceView android:id="@+id/surface_camera"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />
</FrameLayout>

Manifest File

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

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="CameraStackActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Check if it still doesnt work out for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜