首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android图像旋转在不同设备之间发生变化

Android图像旋转在不同设备之间发生变化
EN

Stack Overflow用户
提问于 2017-03-06 22:54:15
回答 1查看 867关注 0票数 2

我正在尝试拍摄一张照片,并将位图设置为图像视图。但是得到的位图在不同的设备上会有不同的旋转。

一个是三星j5 prime (180度),一个是LG G3 (我根据这个设备写的代码很正常),两个都是Android6.0(实际上samsuung是6.0.1,但我不认为这有什么区别)

这是我的代码:

代码语言:javascript
复制
private int findImageRotation(Uri uri) throws IOException {
    ExifInterface ei = new ExifInterface(uri.getPath());
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
    int degree = 0;

    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            degree = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            degree = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            degree = 90;
            break;
        case ExifInterface.ORIENTATION_NORMAL:
        default:
            break;
    }

    return degree;
}

private Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

private void imageToBase64(final Uri uri, final Base64ConvertListener listener) {
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            Bitmap bitmap;
            try {
                int degree = findImageRotation(uri);
                bitmap = BitmapFactory.decodeStream(getContext().getContentResolver().openInputStream(uri));
                bitmap = rotateImage(bitmap, degree - 90);
                if (bitmap != null) {
                    String extension = getMimeType(getContext(), uri);
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                    if (extension.toLowerCase().contains("png"))
                        bitmap.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
                    else if (extension.toLowerCase().contains("jpg") || extension.toLowerCase().contains("jpeg"))
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);

                    ivHostImage.setImageBitmap(bitmap);
                    byte[] byteArray = byteArrayOutputStream.toByteArray();
                    listener.onConvertedToBase64("data:" + extension + ";base64," + Base64.encodeToString(byteArray, Base64.DEFAULT));

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}
EN

回答 1

Stack Overflow用户

发布于 2017-03-06 23:24:02

你试过手动旋转它们吗?

代码语言:javascript
复制
 ExifInterface exif;

           try {

             exif = new ExifInterface(photoPath);//uri.getPath()

              } 

           catch (IOException e) {

            e.printStackTrace();

            exif = null;

              }

            if(exif!=null){

            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);   

            }

          //get Bitmap
          Bitmpam bitm = ..... ;
    //i think in your example
    //is like :  bitmap = BitmapFactory.decodeStream(getContext().getContentResolver().openInputStream(uri));


           if(orientation==8){//exception1
               Matrix matrix = new Matrix();
               matrix.postRotate(270);
               bitm = Bitmap.createBitmap(bitm, 0, 0, bitm.getWidth(), bitm.getHeight(), matrix, true); 
                    }
           if(orientation==6){//exception2
               Matrix matrix = new Matrix();
               matrix.postRotate(90);
               bitm = Bitmap.createBitmap(bitm, 0, 0, bitm.getWidth(), bitm.getHeight(),matrix,true);
                    }
           if(orientation==3){//exception3
               Matrix matrix = new Matrix();
               matrix.postRotate(180);
               bitm = Bitmap.createBitmap(bitm, 0, 0, bitm.getWidth(), bitm.getHeight(),matrix,true);
               }

这对我很管用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42628678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档