I am trying to open Both my front and back Camera but only one is opening and I want to open both at a same time.
Code is below :
public class MainActivity extends Activity implements SurfaceHolder.Callback {
TextView testView;
static Camera camera = null;
SurfaceView surfaceView, surfaceView1;
SurfaceHolder surfaceHolder, surfaceHolder1;
PictureCallback rawCallback;
ShutterCallback shutterCallback;
PictureCallback jpegCallback;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceView1 = (SurfaceView) findViewById(R.id.surfaceView1);
surfaceHolder1 = surfaceView1.getHolder();
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
surfaceHolder.addCallback(this);
surfaceHolder1.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_HARDWARE);
surfaceHolder1.setType(SurfaceHolder.SURFACE_TYPE_HARDWARE);
// for saving image
jpegCallback = new PictureCallback() {
@SuppressLint({ "SdCardPath", "ShowToast" })
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format(
"/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Toast.makeText(getApplicationContext(), "Picture Saved", 2000)
.show();
refreshCamera1();
refreshCamera();
}
};
}
public void captureImage(View v) throws IOException {
// take the picture
camera.takePicture(null, null, jpegCallback);
}
public void refreshCamera() {
if (surfaceHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
camera = camera.open();
} catch (Exception e) {
System.out.println("exception on surface 2 is >>>>>" + e);
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
}
}
public void refreshCamera1() {
if (surfaceHolder1.getSurface() != null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
camera.open();
} catch (Exception e) {
System.out.println("exception on surface 1 is >>>>>" + e);
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
camera.setPreviewDisplay(surfaceHolder1);
camera.startPreview();
} catch (Exception e) {
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
refreshCamera1();
refreshCamera();
}
public void surfaceCreated(SurfaceHolder holder) {
try {
// open the camera
// camera = Camera.open();
boolean found = false;
int i;
for (i = 0; i < Camera.getNumberOfCameras(); i++) {
Camera.CameraInfo newInfo = new Camera.CameraInfo();
Camera.getCameraInfo(i, newInfo);
if (newInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
found = true;
// break;
}
if (found == true) {
camera = Camera.open(i);
}
}
} catch (RuntimeException e) {
// check for exceptions
System.err.println(e);
return;
}
Camera.Parameters param;
param = camera.getParameters();
// modify parameter
param.setPreviewSize(352, 288);
camera.setParameters(param);
try {
// The Surface has been created, now tell the camera where to draw
// the preview.
camera.setPreviewDisplay(surfaceHolder);
camera.setPreviewDisplay(surfaceHolder1);
camera.startPreview();
} catch (Exception e) {
// check for exceptions
System.err.println(e);
return;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// stop preview and release camera
camera.stopPreview();
camera.release();
camera = null;
}
}
I have two SurfaceView in my XML file and when I run the application only one surfaceView is opening and the other surfaceView is blank Any help on this I am stuck here. Thank you.