从相机旋转照片(SAMSUNG设备) [英] Photo rotated from camera (SAMSUNG device)

查看:128
本文介绍了从相机旋转照片(SAMSUNG设备)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我讨厌这家公司.他们所有的设备都有很多错误.好的问题: 林试图解决愚蠢的问题(据我所知存在超过5年) 从相机拍摄的照片-旋转了90度. 我有两个设备:

  Nexus 5p and Samsung j2  
  Nexus - work perfect. Everything fine. 
  Samsung - photo rotated

例如:

Photo size - nexus : Portrate : width 1000, height 1900.  Landscape :
width 1900 , height 1000

让我们在三星设备上查看:

Photo size  - Portrate: width 1900(?????) height - 1000(????)
rotate to landscape : width 1900 height 1000

经过一些测试:如果在三星设备上以横向模式拍摄照片-一切正常.照片未旋转

如果以PORTRATE拍摄照片-照片旋转90度. (但照片的大小与在风景上一样(如何显示)?

任何人都知道如何解决这个愚蠢的错误吗?也许有人可以告诉我如何检测相机的方向?我正在使用IntentActivity拍摄照片:

String _path = Environment.getExternalStorageDirectory()
                                    + File.separator + "camera_img.jpg";
                            File file = new File(_path);
                            Uri outputFileUri = Uri.fromFile(file);
                            Intent intent = new Intent(
                                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                            startActivityForResult(intent, CAMERA_REQUEST);

有什么帮助吗? 我还添加了一个检查器,如果它的三星设备不如旋转.但是只有当我们在纵向模式下创建照片时,旋转效果才不错.在风景上一切都很好.所以我需要以某种方式检测在哪个方向的照片中创建.有人知道吗?

解决方案

UPD 29.08.2018 大家好,我检测到该方法不适用于基于android 8+的三星设备.我没有三星s8(例如),不明白为什么这又不起作用.如果有人可以测试并检查为什么此方法不起作用-请尝试将其修复在一起.

我找到了解决方法:真的很愚蠢,对我来说很难

第一步获取活动结果

 @Override
    protected void onActivityResult(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";
            String p1 = Environment.getExternalStorageDirectory().toString();
            String fName = "/TakenFromCamera.jpg";
            final int rotation = getImageOrientation(_path);
            File file = resaveBitmap(p1, fName, rotation);
            Bitmap mBitmap = BitmapFactory.decodeFile(_path);

main在更改文件之前先隐藏其:getImageOrientation.

1)getImageOrientation(按路径)
2)重新保存文件(如果需要发送到服务器,如果只需要预览,我们可以跳过此步骤)
3)从文件中获取正确的位图

仅需进行步骤1和3即可进行预览,并使用此功能-只需旋转位图即可.

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

getImageOrientation

public static int getImageOrientation(String imagePath) {
    int rotate = 0;
    try {
        ExifInterface exif = new ExifInterface(imagePath);
        int orientation = 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;
}

并在需要时重新保存Bitmap

private File resaveBitmap(String path, String filename, int rotation) { //help for fix landscape photos
        String extStorageDirectory = path;
        OutputStream outStream = null;
        File file = new File(filename);
        if (file.exists()) {
            file.delete();
            file = new File(extStorageDirectory, filename);
        }
        try {
            // make a new bitmap from your file
            Bitmap bitmap = 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 = new FileOutputStream(path + filename);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

仅此而已:)一切正常

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 - rotated on 90 degree. I have two devices :

  Nexus 5p and Samsung j2  
  Nexus - work perfect. Everything fine. 
  Samsung - photo rotated

For example :

Photo size - nexus : Portrate : width 1000, height 1900.  Landscape :
width 1900 , height 1000

Lets see on samsung device :

Photo size  - Portrate: width 1900(?????) height - 1000(????)
rotate to landscape : width 1900 height 1000

After some testing : if make photo in landscape mode on samsung device - than everything ok. Photo not rotated

If make photo in PORTRATE - photo rotated on 90 degree. (BUT size of photo as on landscape (HOW ITS POSSIBLE) ?

Anyone know how to fix this stupid bug ? Maybe any can tell me how to detect orientation for camera ? Im using IntentActivity for photo :

String _path = Environment.getExternalStorageDirectory()
                                    + File.separator + "camera_img.jpg";
                            File file = new File(_path);
                            Uri outputFileUri = Uri.fromFile(file);
                            Intent intent = new Intent(
                                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                            startActivityForResult(intent, CAMERA_REQUEST);

Any help ? I also add a checker , if its samsung device than rotate. But rotation good only if we create photo in portrate mode. In landscape everything fine. So i need somehow detected in which orientation photo was created. Any one know ?

解决方案

UPD 29.08.2018 Hello guys, i detect that this method doesn't work with samsung device based on android 8+. I dont have samsung s8 (for example) and can't understand why this again not work there. If someone can test and check why this not work - lets try to fix this together.

I found how to fix : well its realy stupid and very hard for me

First step get activity result

 @Override
    protected void onActivityResult(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";
            String p1 = Environment.getExternalStorageDirectory().toString();
            String fName = "/TakenFromCamera.jpg";
            final int rotation = getImageOrientation(_path);
            File file = resaveBitmap(p1, fName, rotation);
            Bitmap mBitmap = BitmapFactory.decodeFile(_path);

main stesps its : 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 enough only step 1 and 3, and using this function - just rotate bitmap.

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

getImageOrientation

public static int getImageOrientation(String imagePath) {
    int rotate = 0;
    try {
        ExifInterface exif = new ExifInterface(imagePath);
        int orientation = 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 photos
        String extStorageDirectory = path;
        OutputStream outStream = null;
        File file = new File(filename);
        if (file.exists()) {
            file.delete();
            file = new File(extStorageDirectory, filename);
        }
        try {
            // make a new bitmap from your file
            Bitmap bitmap = 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 = new FileOutputStream(path + filename);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

thats all :) and everything work fine

这篇关于从相机旋转照片(SAMSUNG设备)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆