释放相机实例之后的单个图像被捕获并返回到父活动 [英] Releasing camera instance after a single image is captured and returning to parent activity
问题描述
我试图调用相机
例如,拍摄图像,并返回到上一级的活动。我能够创建并调用相机
实例,捕获和存储图像,但与相机的活动不会退出。
I am trying to call camera
instance, capture an image and return to the parent activity. I am able to create and call the camera
instance, capture and store the image, but the activity with the camera does not exit.
在 preVIEW
活动创建一个摄像头实例,并实现了像的onPause方法()
, onResume()
和 resetCam()
。在code是:
The Preview
activity creates a camera instance and implements methods like onPause()
, onResume()
and resetCam()
. The code is:
//capture button
buttonClick.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
camera.takePicture(shutterCallback, rawCallback, pngCallback);
}
}
protected void onResume() {
super.onResume();
int numCams = Camera.getNumberOfCameras();
if(numCams > 0){
try{
camera = Camera.open(0);
camera.startPreview();
preview.setCamera(camera);
} catch (RuntimeException ex){
Toast.makeText(ctx, getString(R.string.camera_not_found), Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onPause() {
if(camera != null) {
camera.stopPreview();
preview.setCamera(null);
camera.release();
camera = null;
}
super.onPause();
}
private void resetCam() {
camera.startPreview();
preview.setCamera(camera);
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
}
};
PictureCallback pngCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//save the iamge here
resetCam();
Log.d(TAG, "onPictureTaken - png");
}
};
一个捕捉事件后,控制应退还给父活动图像捕捉
这就要求 preVIEW
按:
Intent intent = new Intent(PhotoCapture.this,Preview.class);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
和执行 onActivityResult()
的一样。
我应该如何释放相机
实例安全出口?
How should I release camera
instance for safe exit?
我要回图像的路径保存。
I want to return the path of image saved as well.
感谢您!
修改
我试图调用finish();是这样的:
I tried to call finish(); this way:
buttonClick.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
camera.takePicture(shutterCallback, rawCallback, pngCallback);
Log.d("Tag","4");
finish();
}
});
我把 Log.d()
获取控制流:
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d("Tag","1");
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Tag","2");
}
};
PictureCallback pngCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Tag","3");
//save image
resetCam();
Log.d(TAG, "onPictureTaken - png");
}
};
日志是:
02-19 14:00:28.046 4680-4680/preview.preview D/Tag﹕ 4
02-19 14:00:38.414 4680-4686/preview.preview W/Camera﹕ Camera server died!
没有日志标签:1或标签:2或标签:3
There is no log for Tag:1 or Tag: 2 or Tag: 3!
因此,活动结束后的实际拍摄图像之前/储存。
So the activity is finished before the actual image is captured/stored.
尽管我设置(result_ code = OK)图像不会被保存。
Even though i set (result_code=OK) the image is not saved.
我该如何解决?
推荐答案
打电话到完成()
一旦你完成拍摄照片,它会退出你当前活动,您将返回到previous活动。
make a call to finish()
once you are done taking a photo, it will exit you from current activity and you will be returned back to previous activity.
这篇关于释放相机实例之后的单个图像被捕获并返回到父活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!