. lang。RuntimeException:在mCamera上拍摄失败。takePicture安卓

时间:2021-01-07 18:53:14

I want to capture photos continuously so that every time user don't have to press the button.

我想要连续捕捉照片,这样用户就不用按下按钮了。

My code

我的代码

public class CameraView extends Activity implements SurfaceHolder.Callback, OnClickListener {
private static final String TAG = "CameraTest";
Camera mCamera;
boolean mPreviewRunning = false;
String currentDateTimeString;
Timer myTimer;
ImageView img;
static boolean flagShowPic = true;
public static int TIME_BET_SHOOT = 1000;

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    try {
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.cameraview);

        img = (ImageView) findViewById(R.id.blankImage);

        mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        mSurfaceView.setOnClickListener(this);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {

    public void onPictureTaken(byte[] data, Camera camera) {
        if (data != null) {
            try {
                currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
                currentDateTimeString = currentDateTimeString.replace(" ", "_");
                currentDateTimeString = currentDateTimeString.replace(":", "");
                File f = new File(CaptureCameraImage.imageSavePath + currentDateTimeString + ".png");
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(data);
                fo.close();
                if (flagShowPic == true) {
                    BitmapFactory.Options opts = new BitmapFactory.Options();
                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
                    bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, false);
                    img.setImageBitmap(bitmap);
                }
                mCamera.startPreview();
            } catch (Exception e) {
                e.printStackTrace();
            }
            setResult(585);
        }
    }
};

//  protected void onStop() {
//      Log.e(TAG, "onStop");
//      super.onStop();
//      if (null != myTimer) {
//          myTimer.cancel();
//      }
//      if (null != mCamera) {
//          mCamera.stopPreview();
//          mPreviewRunning = false;
//          mCamera.release();
//      }
//  }

public void surfaceCreated(SurfaceHolder holder) {
    Log.e(TAG, "surfaceCreated");
    try {
        mCamera = Camera.open(CaptureCameraImage.cameraID);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    Log.e(TAG, "surfaceChanged");
    try {
        if (mPreviewRunning) {
            //mCamera.stopPreview();
        }

        Camera.Parameters p = mCamera.getParameters();
        p.setPreviewSize(300, 300);

        if (CaptureCameraImage.cameraID == 0) {
            String stringFlashMode = p.getFlashMode();
            if (stringFlashMode.equals("torch"))
                p.setFlashMode("on"); // Light is set off, flash is set to
            // normal 'on' mode
            else
                p.setFlashMode("torch");
        }

        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
        mPreviewRunning = true;

        myTimer = new Timer();
        MyTimerTask myTimerTask = new MyTimerTask();
        myTimer.scheduleAtFixedRate(myTimerTask, 1000, TIME_BET_SHOOT);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.e(TAG, "surfaceDestroyed");
//      mCamera.stopPreview();
//      mPreviewRunning = false;
//      mCamera.release();
}

private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;

private class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mPreviewRunning == true)
                    mCamera.takePicture(null, mPictureCallback, mPictureCallback);
            }
        });
    }
}

@Override
public void onClick(View v) {
}
}

I'm getting exception

我例外

 FATAL EXCEPTION: main java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1194)
at android.hardware.Camera.takePicture(Camera.java:1139)
at com.androidmyway.demo.capturecameraimage.CameraView$MyTimerTask$1.run(CameraView.java:152)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)

1 个解决方案

#1


2  

use this to open camera. to avoid errors while opening camera.

用这个来打开相机。在打开相机时避免错误。

private void safeCameraOpen(int id) {

        try {
            releaseCameraAndPreview();
            mCamera = Camera.open(id);
        } catch (Exception e) {
            Log.e(getString(R.string.app_name), "failed to open Camera");
            e.printStackTrace();
        }

    }

private void releaseCameraAndPreview() {
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }

#1


2  

use this to open camera. to avoid errors while opening camera.

用这个来打开相机。在打开相机时避免错误。

private void safeCameraOpen(int id) {

        try {
            releaseCameraAndPreview();
            mCamera = Camera.open(id);
        } catch (Exception e) {
            Log.e(getString(R.string.app_name), "failed to open Camera");
            e.printStackTrace();
        }

    }

private void releaseCameraAndPreview() {
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }