2015-08-27 36 views
0

我正在構建一個應用程序。如何通過自定義相機爲android中的所有設備捕捉圖像旋轉

我使用自定義相機捕捉圖像,但捕獲圖像後,我使用輸出流保存它。在下一個活動中,如果使用路徑將相同的圖像放在圖像視圖上而不是旋轉。某些設備是90度,在某些設備中,前置攝像頭爲180度,後置攝像頭爲180度。 我有保存圖像,但其返回0總是

我的代碼通過自定義相機保存圖像之前使用ExifInterface是:

class SaveAsync extends AsyncTask<Void,Void,Void>{ 

     private Context context; 
     private byte[] bytes; 
     private File imageFile; 

     // here bytes is for capture image bytes 
     SaveAsync(Context context,byte[] bytes,File imageFile){ 
      this.context=context; 
      this.bytes=bytes; 
      this.imageFile=imageFile; 
     } 

     @Override 
     protected Void doInBackground(Void... voids) { 
      OutputStream output = null; 

      try { 
       output = new FileOutputStream(imageFile); 
       output.write(bytes); 

       output.flush(); 
       output.close(); 

       sendBroadcast(new Intent(
         Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri 
         .parse("file://" + imageFile))); 


      }catch(IOException ex){ 
       ex.printStackTrace(); 
       Log.e("Camera2","IO in async : "+ex); 
       //Toast.makeText(context,"File not find",Toast.LENGTH_SHORT).show(); 
      }catch (Exception ex){ 
       Log.e("Camera2","Exp in async : "+ex); 
      } 

      return null; 
     } 
    } 

回答

0

試試這個。

  1. 將您的文件轉換爲位圖
  2. 旋轉位圖
  3. 保存旋轉後的位從存儲
+0

我試過這個,但情況是,如果我有兩個移動A和B,如果我在我的代碼中旋轉90度的圖像比它適用於移動A,但是在移動B的情況下它將圖像旋轉180度。 ...所以我需要所有設備的通用代碼..謝謝 –

+0

您可以使用** ExIfInterface **類來檢查位圖圖像。 – Pankaj

0

這裏就存放到文件

  • 負載是可以使用的方法檢查圖像旋轉:

    private Bitmap checkRotation(String filePath, Bitmap scaledBitmap) { 
         ExifInterface exif; 
         try { 
          exif = new ExifInterface(filePath); 
    
          int orientation = exif.getAttributeInt(
            ExifInterface.TAG_ORIENTATION, 0); 
          Log.d("EXIF", "Exif: " + orientation); 
          Matrix matrix = new Matrix(); 
          if (orientation == 6) { 
           matrix.postRotate(90); 
           Log.d("EXIF", "Exif: " + orientation); 
          } else if (orientation == 3) { 
           matrix.postRotate(180); 
           Log.d("EXIF", "Exif: " + orientation); 
          } else if (orientation == 8) { 
           matrix.postRotate(270); 
           Log.d("EXIF", "Exif: " + orientation); 
          } 
          scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, 
            scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, 
            true); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
         return scaledBitmap; 
        } 
    
  • 相關問題