Android相机的图像在我的应用程序中以水平模式保存

时间:2015-03-29 10:29:58

标签: android camera

我用android制作了一个简单的相机应用程序。但有一个问题是我无法解决的。我的相机应用程序的SurfaceView处于纵向模式,但是,当我拍摄图像并将图像保存在文件夹中时。它未以纵向模式保存。因为,我是Android开发的新手,我需要一些帮助。我在下面放了一些代码。

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

            FileOutputStream outStream = null;
            Calendar c = Calendar.getInstance();
            File videoDirectory = new File(path);

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

            try {
                // Write to SD Card
                outStream = new FileOutputStream(path + c.getTime().getSeconds() + ".jpg");
                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) {

            }

            image.setImageBitmap(realImage);



            fotoButton.setClickable(true);
            camera.startPreview();
            progressLayout.setVisibility(View.GONE);
            exitButton.setClickable(true);

        }
    };

    public static Bitmap rotate(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, false);
    }

1 个答案:

答案 0 :(得分:0)

尝试下面的方法,有些时候某些设备的方向旋转到90或180或270这里的方法是尝试这样做以使图像在正常模式下旋转:

public static Bitmap RotateImage(String imagePath,  boolean rotateImage) {

        Matrix imageMatrix = new Matrix();
        if (rotateImage) {
            int rotationNeeded = getImageRotationDegrees(imagePath);
            if (rotationNeeded != 0) {
                imageMatrix.postRotate(rotationNeeded);
            }
        }

    public static int getImageRotationDegrees(String imagePath) {
        int currentRotation = getImageOrientation(imagePath);
        switch (currentRotation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            return 90;
        case ExifInterface.ORIENTATION_ROTATE_180:
            return 180;
        case ExifInterface.ORIENTATION_ROTATE_270:
            return 270;
        }
        return 0;
    }

    public static int getImageOrientation(String imagePath) {
        ExifInterface exifInterface;
        try {
            exifInterface = new ExifInterface(imagePath);
        } catch (IOException e) {
            return ExifInterface.ORIENTATION_UNDEFINED;
        }
        return exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);
    }