android-拍照后恢复camera.preview()
作者:互联网
我有一个Surfaceview,它正在显示相机预览,我要拍照并显示在Surfaceview上,但是当我按“后退”按钮时,它关闭了应用程序,但我希望它显示原始的相机预览.
我也只想在显示图片时重置显示,并且在显示相机预览时通常将功能返回到“后退”按钮.
public class cameraView extends Activity implements SurfaceHolder.Callback{
private SurfaceView preview=null;
private SurfaceHolder previewHolder=null;
private Camera camera=null;
private ImageButton bt = null;
private Toast t = null;
private Camera.Parameters param = null;
private Button b = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cam);
t = Toast.makeText(this, "Just Click The Magnifying Glass To Search", 5000);//creates a new pop up message that lasts for 5 seconds
t.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
t.show();
b = (Button)findViewById(R.id.test);
b.setOnClickListener(search);
bt = (ImageButton)findViewById(R.id.button);//creates instance of button
bt.setOnClickListener(search);//starts an on click listener for button
preview=(SurfaceView)findViewById(R.id.myview);//creates instance of surfaceview
previewHolder=preview.getHolder();//creates a surfaceholder
previewHolder.addCallback(this);//sets surfaceholder callback as the activity
previewHolder.setType(3);//sets the type to SURFACE_TYPE_PUSH_BUFFERS
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { //creates a method that is called automatically when the surface is changed
// TODO Auto-generated method stub
Camera.Parameters param = camera.getParameters();//sets param to be equal to camera parametors
param.setPreviewSize(width, height);//sets width and height to that of what is passed back to it when callback calls it
//param.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(param);//sets the camera parameters to param
camera.startPreview();//starts the preview
camera.autoFocus(cb);//calls autofocus callback method
}
public void surfaceCreated(SurfaceHolder holder) {//called when the surface has been created
// TODO Auto-generated method stub
camera = Camera.open();//opens the camera and sets it to the camera variable
try{
camera.setPreviewDisplay(previewHolder);//sets the display area to previewHolder
}catch(Throwable t){
Log.e(""+t, null);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {//called when sureface is destroyed or when activity is closed
// TODO Auto-generated method stub
camera.stopPreview();//stops the preview
camera.release();//releases the camera
camera = null;// clears the camera so it contains no information
}
AutoFocusCallback cb = new AutoFocusCallback(){
public void onAutoFocus(boolean success, Camera camera) {
// TODO Auto-generated method stub
//return true;
}
};
private OnClickListener search = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
switch(v.getId()){
case R.id.test:
param = camera.getParameters();
if(param.getFlashMode().equals(Parameters.FLASH_MODE_TORCH)){
/*
* IF statement to check the current flash mode and change it appropriately
*/
param.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(param);
}else{
param.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(param);
}
break;
case R.id.button:
camera.autoFocus(cb);//calls autofocus with call back of cb
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
camera.takePicture(null, mPictureCallback, mPictureCallback);
}
}, 2000);
break;
}
}
};
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
bt.setVisibility(4);
b.setVisibility(4);
b.setEnabled(false);
bt.setEnabled(false);
}
};
上面是我的整个活动代码(减去导入次数).
任何帮助是极大的赞赏.
解决方法:
您需要做的是重写onBackPressed()方法:
@Override public void onBackPressed()
{
// Restart camera
}
标签:android-camera,android 来源: https://codeday.me/bug/20191208/2087924.html