0

I made a simple camera application with android. But there is one problem that I cannot solve. My camera app's surfaceView is in portrait mode , however, when I take the image and save the image in the folder. It's not saved in a portrait mode. Since, I'm new to android development I would need some help. I've put some code below.

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);
    }
Jennifer
  • 1,822
  • 2
  • 20
  • 45
  • Try setting the activity as portrait in the android manifest. – King Mar 29 '15 at 10:42
  • Thank you for the comment, however, that is not the problem since its already set that way. Everything looks fine until I see the image in the folder. – Jennifer Mar 29 '15 at 11:44
  • Oh, have you looked at this: http://stackoverflow.com/questions/16128608/camera-preview-is-in-portrait-mode-but-image-captured-is-rotated – King Mar 29 '15 at 11:51

1 Answers1

0

try below methods some times some device have orientantion roated to 90 or 180 or 270 here is the methods try this to get image in normal mode if its gets rotated:

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);
    }
Pankaj
  • 7,908
  • 6
  • 42
  • 65