So I have a very basic camera implementation. My goal is to automatically switch between the front and back cameras every 10 second, until stopped by click of button.
Here is my MainActivity
:
public class MainActivity extends Activity {
private Camera mCamera = null;
private CameraView mCameraView = null;
private CountDownTimer countDownTimer;
private int mCamId = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startCam();
if(mCamera != null) {
mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
camera_view.addView(mCameraView);//add the SurfaceView to the layout
}
//btn to close the application
ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose);
imgClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.exit(0);
}
});
}
private void startCam() {
try{
//you can use open(int) to use different cameras
mCamId = mCamId == 0 ? 1 : 0;
mCamera = Camera.open(mCamId);
switchCam();
} catch (Exception e){
Log.d("ERROR", "Failed to get camera: " + e.getMessage());
}
}
private void switchCam() {
//10 seconds
countDownTimer = new CountDownTimer(10000, 1000) {
@Override
public void onTick(long l) {
Log.d(TAG, l + " left");
}
@Override
public void onFinish() {
cleanup();
startCam();
}
}.start();
}
public void cleanup() {
Log.i(TAG, "Switching Camera");
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
}
And here is my CameraView
class:
public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraView(Context context, Camera camera){
super(context);
mCamera = camera;
mCamera.setDisplayOrientation(90);
//get the holder and set this class as the callback, so we can get camera data here
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try{
//when the surface is created, we can set the camera to draw images in this surfaceholder
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("ERROR", "Camera error on surfaceCreated " + e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
//before changing the application orientation, you need to stop the preview, rotate and then start it again
if(mHolder.getSurface() == null)//check if the surface is ready to receive camera data
return;
try{
mCamera.stopPreview();
} catch (Exception e){
//this will happen when you are trying the camera if it's not running
}
//now, recreate the camera preview
try{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
//our app has only one screen, so we'll destroy the camera in the surface
//if you are unsing with more screens, please move this code your activity
mCamera.stopPreview();
mCamera.release();
}
The first camera starts with no problem. However, the interface freezes into a static image at the switching of second camera, that is after 10 seconds. I am unable to fix it. Where am I mistaken?
Minimal, Complete, Verifiable and Compilable Code: LINK