在Android上访问相机

时间:2012-05-26 00:47:20

标签: android exception camera

我试图实现相机访问,就像在几页中发布一样。

这是代码

 package view;
 import R;
 import android.app.Activity;
 import android.hardware.Camera;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.FrameLayout;
 public class doInspectionImage extends Activity{
private cameraPreview csv;
private Camera cam;
private String rid;
public void onCreate(Bundle is){
    super.onCreate(is);
    this.rid = getIntent().getStringExtra("reportid");
    setContentView(R.layout.spectware_view_takepicture);
    csv = new cameraPreview(this);
    FrameLayout preview = (FrameLayout)findViewById(R.id.picturepreview);
    preview.addView(csv);
    Button takeAPicture = (Button)findViewById(R.id.takePicture);
    takeAPicture.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            cam = csv.getCamera();
            addText();
        }
    });
}
public void onPause(){
    super.onPause();
    csv.CameraDisable();
}
public void onResume(){
    super.onResume();
    csv = new cameraPreview(this);
    FrameLayout preview = (FrameLayout)findViewById(R.id.picturepreview);
    preview.addView(csv);
    Button takeAPicture = (Button)findViewById(R.id.takePicture);
    takeAPicture.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            cam = csv.getCamera();
            addText();
        }
    });
}
private void addText(){
    setContentView(R.layout.spectware_view_picturetext);
    Button storePic = (Button)findViewById(R.id.storepicture);
    storePic.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            EditText dt = (EditText)findViewById(R.id.picturetext);
            storePicture(dt.getText().toString());
            finish();
        }
    });
}
private void storePicture(String txt){
    cam.takePicture(null,null,new model.camera.Camera(rid,txt));
}
}

包视图;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder holder;
private Camera camera;
public cameraPreview(Context context){
    super(context);
    this.holder = this.getHolder();
    this.holder.addCallback(this);
    this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder){
        try{
            //Open the Camera in preview mode
            this.camera = Camera.open();
            this.camera.setPreviewDisplay(this.holder);
        }
        catch(IOException ioe)
        {
                ioe.printStackTrace(System.out);
        }
}
public void CameraEnable(SurfaceHolder holder){
    try{
        //Open the Camera in preview mode
        this.camera = Camera.open();
        this.camera.setPreviewDisplay(this.holder);
    }
    catch(IOException ioe)
    {
            ioe.printStackTrace(System.out);
    }
}
public void CameraDisable(){
    this.camera.release();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(width, height);
        camera.setParameters(parameters);
        camera.startPreview();
}
public void surfaceDestroyed(SurfaceHolder holder){
        // Surface will be destroyed when replaced with a new screen
        //Always make sure to release the Camera instance
        camera.stopPreview();
        camera.release();
        camera = null;
}
public Camera getCamera(){
        return this.camera;
}
public void onPause(){

}
}

一旦我用

开始此活动
    Intent i = new Intent();
i.setClass(l,doInspectionImage.class);
i.putExtra("reportid",l.inspection_id);
startActivity(i);

我在记录器中收到此消息:

    05-26 02:35:12.550: W/dalvikvm(8300): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
    05-26 02:35:12.600: E/AndroidRuntime(8300): FATAL EXCEPTION: main
    05-26 02:35:12.600: E/AndroidRuntime(8300): java.lang.RuntimeException: Fail to connect to camera service
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.hardware.Camera.native_setup(Native Method)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.hardware.Camera.<init>(Camera.java:300)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.hardware.Camera.open(Camera.java:275)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at view.cameraPreview.surfaceCreated(cameraPreview.java:22)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.SurfaceView.updateWindow(SurfaceView.java:569)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.SurfaceView.updateWindow(SurfaceView.java:407)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:352)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.View.draw(View.java:6936)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.View.draw(View.java:6936)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1917)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewRoot.draw(ViewRoot.java:1530)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1266)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.os.Handler.dispatchMessage(Handler.java:99)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.os.Looper.loop(Looper.java:130)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.app.ActivityThread.main(ActivityThread.java:3691)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at java.lang.reflect.Method.invoke(Method.java:507)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at dalvik.system.NativeStart.main(Native Method)

我尽可能地接近样品。 我不知道我做了什么不同,除了我必须做出的改变,以实现它的目的。 我感谢任何帮助。

克里斯

P.S。:如果我点击后退按钮,我会得到预览。 但是在应用程序崩溃之后的第二秒。

1 个答案:

答案 0 :(得分:0)

要检查的两件事

  1. 检查清单中的相机权限。确保添加。

  2. 您必须正确释放相机资源(onPause中的stopreview)。