2017-03-06 5 views
2

私は写真を撮り、ビットマップをイメージビューに設定しようとしています。しかし、結果のビットマップは、異なるデバイスで異なって回転します。デバイスからデバイスへのAndroidイメージのローテーションが変更される

一つは、SAMSUNG J5プライム(180度)である 一つは、LGのG3である(私はこの装置によれば、コードを書いたように、通常の) はどちらも(アンドロイド6.0を持って、実際にsamsuungは6.0.1を持っていますが、私はそれを考えていませんどんな違いが)

これは私のコードです:

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

答えて

1

私はこの問題を数日間も無駄にしていました。最後に私はこの答えを見つけました。 See it.

+1

将来の参照のために、絶対パスを取得する。 [リンク](http://stackoverflow.com/a/20559175/5973493) – msecilmis

0

は、あなたがそれらを回転させてmanualyしようとしたことがありますか?

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

いいえ、まだ2つのデバイスの間に180度の違いがあります。しかし、ありがとうございます。ありがとう、それは動作します。 – msecilmis

関連する問題