显示Camera Preview时出错

时间:2013-10-10 18:46:55

标签: android android-camera surfaceview

我正在尝试打开相机预览但我得到了

  

FATAL EXCEPTION:AsyncTask#1 java.lang.RuntimeException:错误   执行doInBackground()

时发生

以下是我的代码:

public class ScanActivity extends Activity implements SurfaceHolder.Callback, OnClickListener {

private SurfaceHolder surface_holder;

protected Camera camera;
protected boolean scanning;

private Animation laser_effect;
private Animation flash_effect;

public static final class id {
    public static final int quit = 0;
    public static final int found = 1;
    public static final int nothing = 2;
    public static final int error = 3;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_scan);
    findViewById(R.id.capture_button).setOnClickListener(this);![enter image description here][1]   

}

@Override
protected void onResume() {
    super.onResume();
    /* open camera if any, returns error otherwise */
    camera = Camera.open();
    if (camera == null) {
        Intent error = new Intent();
        error.putExtra("reason", "Camera Error");
        setResult(id.error, error);
        finish();
    }


    restartView();
    surface_holder = ((SurfaceView) findViewById(R.id.surface_view)).getHolder();
    surface_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    surface_holder.addCallback(this);
}


@Override
protected void onPause() {
    this.scanning=false;
    /* release camera */
    camera.cancelAutoFocus();
    camera.stopPreview();
    camera.release();
    super.onPause();
}   

private void restartView() {


    findViewById(R.id.freeze).setVisibility(View.GONE);
    findViewById(R.id.loading).setVisibility(View.GONE);        
    findViewById(R.id.capture_button).setVisibility(View.VISIBLE);
    findViewById(R.id.surface_view).setVisibility(View.VISIBLE);
    findViewById(R.id.camera_loading).setVisibility(View.GONE);
}

@Override
public void onClick(View v) {
    switch(v.getId()) {
    case R.id.capture_button:
        scanning = true;

        break;
    }
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // void implementation
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    findViewById(R.id.camera_loading).setVisibility(View.VISIBLE);
    //we use an AsyncTask to avoid ugly lag when camera is loading...
    new InitCamera().execute(holder); 
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // void implementation
}



private class InitCamera extends AsyncTask<SurfaceHolder, Void, Boolean> {

    @Override
    protected Boolean doInBackground(SurfaceHolder... holder) {
        /* initialize camera parameters and start Preview */
        Rect dim = holder[0].getSurfaceFrame();
        int w = dim.width();
        int h = dim.height();
        Camera.Parameters params = camera.getParameters();
        params.setPreviewSize(w, h);
        camera.setParameters(params);
        try {
            camera.setPreviewDisplay(surface_holder);
        } catch (IOException e) {
            e.printStackTrace();
        }
        camera.startPreview();
        return true;
    }

    @Override
    protected void onPostExecute(Boolean a) {
        findViewById(R.id.camera_loading).setVisibility(View.GONE);
    }
}
}

1 个答案:

答案 0 :(得分:0)

最有可能的是,问题来自于尝试从非UI线程操纵UI。检查堆栈跟踪有助于证实这一理论。 AFAIK,您在doInBackground()中所做的工作应该在主应用程序线程上执行。