0

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.

fadden
  • 51,356
  • 5
  • 116
  • 166
  • It is not possible to open both cameras at a time on most of the phones however on some models from samsung like galaxy s5 it is possible. – Ahmad Nawaz Jun 09 '15 at 05:48
  • I don't think that is possible (as @AhmadNawaz). The reason for this I believe is because the Camera class only supports to have one Camera instance open at a time and this Camera instance has to be released before you can do something with another camera. However for Lollipop and up the Camera class is deprecated, so it may be possible on those Android versions. – Araw Jun 09 '15 at 05:56
  • @Araw as I know i think galaxy s5 have a Dual Camera app in kitkat or jelly beans. http://www.samsung.com/uk/support/skp/faq/1053544 – Ahmad Nawaz Jun 09 '15 at 06:03
  • @AhmadNawaz Hmm, seems so. Found this link OP can take look at though: http://stackoverflow.com/questions/12382322/is-it-possible-to-use-front-and-back-camera-at-same-time-in-android As far as I now can see it is possible as long both camera hardware (front and back) are different. And here is the source https://bitbucket.org/jens_grubert/androiddualcameracapture/src/e4d8199f15f7a802033df02c8d124cc6ce8db1a8/src/at/tugraz/icg/dualcamera/?at=master – Araw Jun 09 '15 at 06:08

0 Answers0