2012-06-26 180 views
1

用戶可以在任何模式下從camera捕捉圖像,但我想限制結果僅限於肖像(結果應僅限於肖像)。從相機拍攝的圖像應該處於肖像模式

要做到這一點我使用ExifInterface和一些擴展我成功過,它的做工精細的LG & HTC設備在三星設備不工作。

代碼:

    ExifInterface exif = new ExifInterface(path); 
       orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 
       Log.e("orientation",""+orientation); 
       Matrix m=new Matrix(); 

       if((orientation==3)){ 

       m.postRotate(180); 
       m.postScale((float)bm.getWidth(), (float)bm.getHeight()); 

//    if(m.preRotate(90)){ 
       Log.e("in orientation",""+orientation); 

       bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true); 
       return bitmap; 
       } 
       else if(orientation==6){ 

        m.postRotate(90); 

        Log.e("in orientation",""+orientation); 

        bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true); 
        return bitmap; 
       } 

       else if(orientation==8){ 

        m.postRotate(270); 

        Log.e("in orientation",""+orientation); 

        bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true); 
        return bitmap; 
       } 
       return bitmap; 
      } 
      catch (Exception e) { 
      } 
      return null; 
     } 

您的建議是明顯的。

回答

0

試試這個工作對我來說

public static Bitmap scaleImage(Context context, Uri photoUri) throws IOException { 
      InputStream is = context.getContentResolver().openInputStream(photoUri); 
      BitmapFactory.Options dbo = new BitmapFactory.Options(); 
      dbo.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(is, null, dbo); 
      is.close(); 

      int rotatedWidth, rotatedHeight; 
      int orientation = getOrientation(context, photoUri); 

      if (orientation == 90 || orientation == 270) { 
       rotatedWidth = dbo.outHeight; 
       rotatedHeight = dbo.outWidth; 
      } else { 
       rotatedWidth = dbo.outWidth; 
       rotatedHeight = dbo.outHeight; 
      } 

      Bitmap srcBitmap; 
      is = context.getContentResolver().openInputStream(photoUri); 
      if (rotatedWidth > 100 || rotatedHeight > 100) { 
       float widthRatio = ((float) rotatedWidth)/((float) 100); 
       float heightRatio = ((float) rotatedHeight)/((float) 100); 
       float maxRatio = Math.max(widthRatio, heightRatio); 

       // Create the bitmap from file 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = (int) maxRatio; 
       srcBitmap = BitmapFactory.decodeStream(is, null, options); 
      } else { 
       srcBitmap = BitmapFactory.decodeStream(is); 
      } 
      is.close(); 

      /* 
      * if the orientation is not 0 (or -1, which means we don't know), we 
      * have to do a rotation. 
      */ 
      if (orientation > 0) { 
       Matrix matrix = new Matrix(); 
       matrix.postRotate(orientation); 

       srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), 
         srcBitmap.getHeight(), matrix, true); 
      } 

      String type = context.getContentResolver().getType(photoUri); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      if (type.equals("image/png")) { 
       srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
      } else if (type.equals("image/jpg") || type.equals("image/jpeg")) { 
       srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
      } 
      byte[] bMapArray = baos.toByteArray(); 
      baos.close(); 
      return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length); 
     } 

     public static int getOrientation(Context context, Uri photoUri) { 
      /* it's on the external media. */ 
      Cursor cursor = context.getContentResolver().query(photoUri, 
        new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null); 

      if (cursor.getCount() != 1) { 
       return -1; 
      } 

      cursor.moveToFirst(); 
      return cursor.getInt(0); 
     } 
     private void openGalleryImage(Intent data) 
     { 
      Uri selectedimg = data.getData(); 
      Uri uriselectedimage=data.getData(); 
      mString=uriselectedimage.getPath(); 
      try 
      { 
       mInputStream= getContentResolver().openInputStream(selectedimg); 
      } 
      catch (FileNotFoundException e) 
      { 
       e.printStackTrace(); 
      } 

      String[] path = { MediaStore.Images.Media.DATA }; 
      Cursor c = getContentResolver().query(selectedimg, path, null, null,null); 
      c.moveToFirst(); 
      int columnIndex = c.getColumnIndex(path[0]); 
      selectedImagePath=c.getString(columnIndex); 
      uri_outputFileUri= Uri.parse(selectedImagePath); 
      c.close(); 
     } 

     private void saveImage(String mPath) 
     { 

      System.out.println("Paths "+mPath); 
       mediaDir= new File(Environment.getExternalStorageDirectory() +"/MyFolder"); 
       mediaDir.mkdir(); 


      File myImage = new File(mediaDir, Long.toString(System.currentTimeMillis()) + ".png"); 

      try 
      { 
       FileOutputStream out = new FileOutputStream(myImage); 
       mbitmap_outputImage=BitmapFactory.decodeFile(mPath);  
       mbitmap_outputImage=Bitmap.createScaledBitmap(mbitmap_outputImage, 390, 310, true); 
       mbitmap_outputImage.compress(Bitmap.CompressFormat.PNG, 100, out); 
       img.setImageBitmap(mbitmap_outputImage); 
       out.flush();  
       out.close(); 

      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 

     } 
相關問題