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

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

问题描述

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

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

例如:

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

如果在 PORTRATE 中制作照片 - 照片旋转 90 度.(但照片的大小与横向一样(如何可能)?

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

有人知道如何修复这个愚蠢的错误吗?也许有人可以告诉我如何检测相机的方向?我使用 IntentActivity 拍照:

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);

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

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 我发现此方法不适用于基于 Android 8+ 的三星设备.我没有三星 s8(例如),不明白为什么这在那儿又不起作用.如果有人可以测试并检查为什么这不起作用 - 让我们一起尝试解决这个问题.

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.

第一步获取活动结果

 @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);

主要步骤是 getImageOrientation 在文件更改之前.

Main steps it's getImageOrientation before changes in file.

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

对于预览,只需执行步骤 1 和 3,并使用此功能 - 只需旋转位图.

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) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return rotatedBitmap;
    }

getImageOrientation

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;
    }

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

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