2015-11-04 1 views
0

J'essayais de copier une image de l'URI vers un chemin de fichier. Puis j'ai lu l'image du chemin, mais l'image que j'ai eu a été tournée de 90 degrés vers le bas. Voici ma fonction. Quelqu'un peut-il aider à cela?image pivotée de 90 degrés lors de la copie de l'image de l'URI vers le fichier

public boolean copyPicture(Context context, Uri source, String dest) { 
    boolean result = false; 
    int bytesum = 0; 
    int byteread = 0; 
    File destFile = new File(dest); 
    String scheme = source.getScheme(); 
    if (ContentResolver.SCHEME_CONTENT.equals(scheme) 
      || ContentResolver.SCHEME_FILE.equals(scheme)) { 
     InputStream inStream = null; 
     try { 
      inStream = context.getContentResolver().openInputStream(source); 
      if (!destFile.exists()) { 
       result = destFile.createNewFile(); 
      } 
      if (result) { 
       FileOutputStream fs = new FileOutputStream(dest); 
       byte[] buffer = new byte[1024]; 
       while ((byteread = inStream.read(buffer)) != -1) { 
        bytesum += byteread; //字节数 文件大小 
        System.out.println(bytesum); 
        fs.write(buffer, 0, byteread); 
       } 
       inStream.close(); 
       fs.flush(); 
       fs.close(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      result = false; 
     } 
    } 
    return result; 
} 
+0

http://stackoverflow.com/questions/33406377/how-to-solve-image-capturing-automatically-rotating-90-degrees-in-android/33406434#33406434 –

+0

Merci Anoop. Mais je veux savoir pourquoi l'image est tournée pendant la fonction de copie. Ou comment puis-je éviter la rotation. –

Répondre

1

EXIF ​​INTERFACE est la réponse. Il vous permet de lire les attributs spécifiés à partir d'un fichier image.

Bitmap bitmap = BitmapFactory.decodeFile(path, options); 
       try { 

         ExifInterface exif = new ExifInterface(path); 
         int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

         int angle = 0; 

         if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { 
          angle = 90; 
         } 
         else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { 
          angle = 180; 
         } 
         else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
          angle = 270; 
         } 
         Matrix mat = new Matrix(); 
         mat.postRotate(angle); 
         Bitmap correctBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);     
        bitmap=correctBmp; 
       } 
       catch(Exception e){ 

       }