gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:52:04 25 4
gpt4 key购买 nike

我正在做一个项目,我想在设备移动时拍照,以创建一个全景图像本身。一旦陀螺力超过阈值,我希望相机拍照。我有故障保险柜,所以一次只能拍摄一张照片,但我不知道为什么会发生故障。

这是我的主要 Activity 代码:

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)

我知道我在主线程上进行了很多操作,但我认为这不足以使程序崩溃。

任何帮助将不胜感激

最佳答案

我想通了......

如果您遇到类似问题,请务必重新开始预览。

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

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




mCamera.startPreview();
safeToTakePicture = true;

关于android - takePicture 在 Android 应用程序上失败(使用 SensorEventListener 调用图片),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37042737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com