2016-05-05 11 views
1

Ich arbeite an einem Projekt, in dem ich ein Bild aufnehmen möchte, während sich das Gerät bewegt, um ein Panoramabild per se zu erstellen. Sobald die gyroskopische Kraft eine Schwelle überschreitet, möchte ich, dass die Kamera ein Foto macht. Ich habe keine Safes, also wird nur ein Bild zu einer Zeit aufgenommen, aber ich weiß nicht, warum es scheitert.takePicture ist in der Android App fehlgeschlagen (Mit SensorEventListener zum Aufrufen von Picture)

hier ist mein Code für die Haupttätigkeit:

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); 
} 

} 

und der Code für die cameraPreview Klasse

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); 
    } 
} 
} 

Mein Fehler ist:

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) 

Ich weiß, ich habe eine Es läuft viel auf dem Hauptthread, aber ich denke nicht, dass es genug ist, um das Programm zum Absturz zu bringen.

Jede Hilfe wäre sehr dankbar

Antwort

2

ich es herausgefunden ....

Wenn Sie ein ähnliches Problem haben, stellen Sie sicher wieder um die Vorschau zu starten.

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

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




     mCamera.startPreview(); 
     safeToTakePicture = true;