Skip to content Skip to sidebar Skip to footer

Photo Rotated From Camera (samsung Device)

i hate this company. All them devices have a lot of bugs. Ok question : Im trying to fix stupid problem (which as i know exist more than 5 years) Its photo taken from camera - rot

Solution 1:

UPD 29.08.2018 I found that this method doesn't work with Samsung device based on Android 8+. I don't have Samsung s8 (for example) and can't understand why this again does not work there. If someone can test and check why this not work - let's try to fix this together.


I found how to fix: well it's really stupid and very hard for me.

First step get activity result

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {

            String_path= Environment.getExternalStorageDirectory() + File.separator + "TakenFromCamera.jpg";
            Stringp1= Environment.getExternalStorageDirectory().toString();
            StringfName="/TakenFromCamera.jpg";
            finalintrotation= getImageOrientation(_path);
            Filefile= resaveBitmap(p1, fName, rotation);
            BitmapmBitmap= BitmapFactory.decodeFile(_path);

Main steps it's getImageOrientation before changes in file.

  1. getImageOrientation (by path)
  2. resave file (if need send to server, if you need only for preview we can skip this step)
  3. get correct bitmap from file

For preview it's enough to perform only steps 1 and 3, and using this function - just rotate bitmap.

private Bitmap checkRotationFromCamera(Bitmap bitmap, String pathToFile, int rotate) {
        Matrixmatrix=newMatrix();
        matrix.postRotate(rotate);
        BitmaprotatedBitmap= Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return rotatedBitmap;
    }

getImageOrientation

publicstaticintgetImageOrientation(String imagePath) {
    introtate=0;
    try {
        ExifInterfaceexif=newExifInterface(imagePath);
        intorientation= exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return rotate;
}

and resaveBitmap if need

private File resaveBitmap(String path, String filename, int rotation) { //help for fix landscape photosStringextStorageDirectory= path;
        OutputStreamoutStream=null;
        Filefile=newFile(filename);
        if (file.exists()) {
            file.delete();
            file = newFile(extStorageDirectory, filename);
        }
        try {
            // make a new bitmap from your fileBitmapbitmap= BitmapFactory.decodeFile(path + filename);
            bitmap = checkRotationFromCamera(bitmap, path + filename, rotation);
            bitmap = Bitmap.createScaledBitmap(bitmap, (int) ((float) bitmap.getWidth() * 0.3f), (int) ((float) bitmap.getHeight() * 0.3f), false);
            bitmap = Utils.getCircleImage(bitmap);
            outStream = newFileOutputStream(path + filename);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

Solution 2:

If you want to rotate an image but you only have the byte array, you can use this:

privatebyte[] rotationFromCamera(byte[] data) {
    introtation= Exif.getOrientation(data);
    if(rotation == 0) return data;
    Bitmapbitmap= BitmapFactory.decodeByteArray(data, 0, data.length, null);
    Matrixmatrix=newMatrix();
    matrix.postRotate(rotation);
    BitmaprotatedBitmap= Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    ByteArrayOutputStreamstream=newByteArrayOutputStream();
    rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

and this exif util from this answer

This is a bit slow if the image is large.

Solution 3:

Found this here, works for me Samsung S8.

staticfinalString[] CONTENT_ORIENTATION = newString[] {
            MediaStore.Images.ImageColumns.ORIENTATION
    };


publicstaticint getExifOrientation(Context context, Uri uri) {
    ContentResolver contentResolver = context.getContentResolver();
    Cursor cursor = null;
    try {
        String id = DocumentsContract.getDocumentId(uri);
        id = id.split(":")[1];
        cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                CONTENT_ORIENTATION, MediaStore.Images.Media._ID + " = ?", newString[] { id }, null);
        if (cursor == null || !cursor.moveToFirst()) {
            return0;
        }
        return cursor.getInt(0);
    } catch (RuntimeException ignored) {
        // If the orientation column doesn't exist, assume no rotation.return0;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

This works when I get the image from Gallery Intent.ACTION_GET_CONTENT.

Tested it with MediaStore.ACTION_IMAGE_CAPTURE, and it returned 0. But maybe I'm doing something wrong.

Solution 4:

Are you taking the picture yourself with the camera API ? If so you can get the orientation with: Camera1 API :

CameraInfocameraInfo=newCameraInfo();
intsensorOrientation= cameraInfo.orientation;

Camera2 API :

CameraManagermanager= (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristicscharacteristics= mCameraCharacteristics;
intsensorOrientation= characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);

Solution 5:

Using Glide lib could solve the issue

@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_PICK_PHOTO_FOR_PROFILE && resultCode == RESULT_OK && data != null && data.getData() != null) {
        try {
           Glide.with(this).load(data.getData()).into(mProfileFragment.mProfileImage);
        } catch (Exception e) {
            Utils.handleException(e);
        }
}

Post a Comment for "Photo Rotated From Camera (samsung Device)"