Android应用程序上的takePicture失败(使用SensorEventListener调用图片)

时间:2016-05-05 05:02:32

标签: android camera computer-vision sensor

我正在开展一个项目,我希望在设备移动时拍照,以便创建全景图像本身。一旦陀螺力超过阈值,我希望相机拍照。我有失败的保险箱,所以一次只拍摄一张照片,但我不知道它为什么会失败。

这是我的主要活动代码:

public class MainActivity extends Activity implements SensorEventListener {

static CameraPreview mPreview;
Camera mCamera;
static TextView pCount;

public SensorManager sensorManager;
public Sensor gyroscope;
public float xGyro;
public float yGyro;
public float zGyro;
public TextView xG;
public TextView yG;
public TextView zG;
public static double PHOTOTHRESHOLD = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);
    pCount = (TextView) findViewById(R.id.pictureCount);

    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    xG = (TextView) findViewById(R.id.xG);
    yG = (TextView) findViewById(R.id.yG);
    zG = (TextView) findViewById(R.id.zG);
}

public static void capture(){
    mPreview.capturePicture();

}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}


@Override
public void onSensorChanged(SensorEvent event) {
    xGyro = event.values[0];
    yGyro = event.values[1];
    zGyro = event.values[2];
    xG.setText(String.valueOf(xGyro));
    yG.setText(String.valueOf(yGyro));
    zG.setText(String.valueOf(zGyro));
    if(xGyro > PHOTOTHRESHOLD || yGyro > PHOTOTHRESHOLD || zGyro > PHOTOTHRESHOLD){
        capture();
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

@Override
protected void onResume(){
    super.onResume();
    sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}

}

和cameraPreview类的代码

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder mHolder;
private Camera mCamera;
public boolean safeToTakePicture;

public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
        safeToTakePicture = true;
    } catch (IOException e) {
        Log.d("CAMERA", "Error setting camera preview: " + e.getMessage());
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.

    if (mHolder.getSurface() == null){
        // preview surface does not exist
        return;
    }

    // stop preview before making changes
    try {
        mCamera.stopPreview();
    } catch (Exception e){
        // ignore: tried to stop a non-existent preview
    }

    // set preview size and make any resize, rotate or
    // reformatting changes here

    // start preview with new settings
    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

    } catch (Exception e){
        Log.d("CAMERA", "Error starting camera preview: " + e.getMessage());
    }
}

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        safeToTakePicture = true;
    }
};


public void capturePicture() {
    if(safeToTakePicture) {
        safeToTakePicture = false;
        mCamera.takePicture(null, null, mPicture);
    }
}
}

我的错误是:

Process: rimodel.streampictures, PID: 21185

java.lang.RuntimeException: takePicture failed

at android.hardware.Camera.native_takePicture(Native Method)

at android.hardware.Camera.takePicture(Camera.java:1436)

at android.hardware.Camera.takePicture(Camera.java:1381)

at rimodel.streampictures.CameraPreview.capturePicture(CameraPreview.java:114)

at rimodel.streampictures.MainActivity.capture(MainActivity.java:70)

at rimodel.streampictures.MainActivity.onSensorChanged(MainActivity.java:98)

at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:405)

at android.os.MessageQueue.nativePollOnce(Native Method)

at android.os.MessageQueue.next(MessageQueue.java:143)

at android.os.Looper.loop(Looper.java:122)

at android.app.ActivityThread.main(ActivityThread.java:5254)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我知道我在主线程上有很多事情,但我认为这不足以让程序崩溃。

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

我想通了......

如果遇到类似问题,请务必再次开始预览。

 private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {




        mCamera.startPreview();
        safeToTakePicture = true;