如何打开前置自定义相机?

时间:2015-01-21 13:28:44

标签: android android-camera

我需要在按钮点击时打开相机,我需要的是当我点击按钮然后后相机打开,点击拍摄照片后,我也需要打开前置摄像头。 请帮帮我。

图片回拨代码:

PictureCallback jpegCallback = new PictureCallback() {
        @SuppressWarnings("deprecation")
        public void onPictureTaken(byte[] data, Camera camera) {

            FileOutputStream outStream = null;
            String filepath = Environment.getExternalStorageDirectory().getPath();
            Calendar c = Calendar.getInstance();
            File videoDirectory = new File(filepath,"Spooks");

            if (!videoDirectory.exists()) {
                videoDirectory.mkdirs();
            }

            try {
                Calendar _cal = Calendar.getInstance();

                long _timeStamp = _cal.getTimeInMillis();
                ////System.out.println( "Time stamp"+_timeStamp);
                String fileToSend = videoDirectory.getAbsolutePath() + "/" +"Image_"+ _timeStamp +".png";
                // Write to SD Card
                System.out.println("Write to SD Card");
                outStream = new FileOutputStream(fileToSend);
                outStream.write(data);
                outStream.close();


            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {

            }


            Bitmap realImage;
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 5;

            options.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared

            options.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future


            realImage = BitmapFactory.decodeByteArray(data,0,data.length,options);
            ExifInterface exif = null;
            try {
                exif = new ExifInterface(path + c.getTime().getSeconds()
                        + ".jpg");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                Log.d("EXIF value",
                        exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                        .equalsIgnoreCase("1")) {
                    realImage = rotate(realImage, 90);
                } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                        .equalsIgnoreCase("8")) {
                    realImage = rotate(realImage, 90);
                } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                        .equalsIgnoreCase("3")) {
                    realImage = rotate(realImage, 90);
                } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                        .equalsIgnoreCase("0")) {
                    realImage = rotate(realImage, 90);
                }
            } catch (Exception e) {

            }


            camera.startPreview();
            //          camera.stopPreview();
            System.out.println("Back cam image saved");
            System.out.println("surfaceDestroyed on activity");

            /*_camera.stopPreview();
            _camera.release();*/

            //          finish();
            //          _camera.release();
            //          _camera.stopPreview();

            //need to open from camera
            _camera.release();
            _camera = null;



            if(_camera==null){

                _camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);

                _camera.startPreview();

                _camera.setErrorCallback(new ErrorCallback() {
                    public void onError(int error, Camera mcamera) {

                        _camera.release();
                        _camera = Camera.open();
                        Log.d("Camera died", "error camera");

                    }
                });
            }
            if (_camera != null) {

                if (Build.VERSION.SDK_INT >= 14)

                setCameraDisplayOrientation(context,
                        CameraInfo.CAMERA_FACING_FRONT, _camera);
                preview.setCamera(_camera);

            }



        }
    };

1 个答案:

答案 0 :(得分:1)

OnPictureTakenCallback开放式前置摄像头中。

 private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

    public static final String TAG = "error";

    @Override
    public void onPictureTaken(byte[] mData, Camera camera) {
       /*
         Here do something with the taken picture, then stop
         the preview, release back camera and open front camera

      */
   }

}

切换相机时要小心,因为在拍摄其他相机之前必须先释放当前相机。

编辑:

open()方法将int参数作为摄像机的ID。如果您在没有传递任何内容的情况下调用open方法,它将打开相机。要打开前置摄像头,您必须将前置摄像头的ID传递给open方法。

Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);