Android 安卓摄像头&x27;s图像以水平模式保存在我的应用程序中

Android 安卓摄像头&x27;s图像以水平模式保存在我的应用程序中,android,camera,Android,Camera,我用android做了一个简单的摄像头应用程序。但有一个问题我无法解决。但是,当我拍摄图像并将图像保存到文件夹中时,我的相机应用程序的surfaceView处于纵向模式。它不是以肖像模式保存的。因为,我是android开发新手,我需要一些帮助。我在下面放了一些代码 PictureCallback jpegCallback = new PictureCallback() { @SuppressWarnings("deprecation") public void o

我用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);
    }

尝试以下方法有时某些设备的方向旋转到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);
    }

尝试在android清单中将活动设置为纵向。感谢您的评论,但是,这不是问题,因为它已经设置为这种方式。在我看到文件夹中的图像之前,一切看起来都很好。哦,你看过这个吗: