2014-01-14 155 views
1

我使用相機API拍照我必須根據我的圖像視圖大小打開不同大小的相機。我正在按照我們在名爲「ApiDemo」的Android sdk/sample/adroid-18中獲得的示例項目進行操作,但我已更改的內容未在setcontentview上設置相機。我已經在Frame Layout上設置了相機。起初我的相機預覽被弄糊了,所以我得到了相機OptimalPreviewSize,並將FrameLayout參數的寬度和高度設置爲wrap-content.Now相機預覽比ImageView小(我想要的大小)。如果我將FrameLayout參數的大小設置爲match-parent,那麼相機View就是stretch.How來解決這個問題。中心裁剪圖像在適當的大小設置ImageView

找到此鏈接更多規格。 Android camera preview look strange

UPDATE

我的相機預覽大小是好的,現在我使用的佈局方法的想法是我有更大的佈局,然後我的ImageView,現在相機預覽看起來很不錯。 現在我面臨的問題是設置合適的大小的圖像,我必須將裁切和縮放中心放在同一大小中,就像我的ImageView.This圖片我通過TakePicture方法獲取並保存在SD卡中。

爲了這個,我用這個方法: -

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) { 
    int sourceWidth = source.getWidth(); 
    int sourceHeight = source.getHeight(); 

    // Compute the scaling factors to fit the new height and width, respectively. 
    // To cover the final image, the final scaling will be the bigger 
    // of these two. 
    float xScale = (float) newWidth/sourceWidth; 
    float yScale = (float) newHeight/sourceHeight; 
    float scale = Math.max(xScale, yScale); 

    // Now get the size of the source bitmap when scaled 
    float scaledWidth = scale * sourceWidth; 
    float scaledHeight = scale * sourceHeight; 

    // Let's find out the upper left coordinates if the scaled bitmap 
    // should be centered in the new size give by the parameters 
    float left = (newWidth - scaledWidth)/2; 
    float top = (newHeight - scaledHeight)/2; 

     // The target rectangle for the new, scaled version of the source bitmap will now 
     // be 
     RectF targetRect = new RectF(left+50, top, left + scaledWidth, top + scaledHeight+50); 
//  RectF targetRect = new RectF(0, 0, newWidth, newHeight/2); 
     // Finally, we create a new bitmap of the specified size and draw our new, 
     // scaled bitmap onto it. 
     Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig()); 
     Canvas canvas = new Canvas(dest); 
     canvas.drawBitmap(source, null, targetRect, null); 

     return dest; 
} 

但結果圖像質量不good.Height角從頂部和底部,結果圖像質量切割不good.Pixels被拉伸。

不要告訴我使用scaleType = Center_crop我不能在我的情況下使用它,並且不希望向用戶顯示裁剪幀,這個全部過程不應該顯示在UI上。

UPDATE

我從中心和規模,根據我的ImageView大小

Bitmap dstBmp = ThumbnailUtils.extractThumbnail(source, newWidth, newHeight); 

,但我得到的位圖不看同在的FrameLayout顯示相機預覽用於農作物圖像的打擊方法。因爲相機預覽很大。我認爲這些代碼裁剪了大面積。 我試圖減少寬度和改變高度,但沒有得到我想要的比例相同的裁剪圖像。

一個更多的想法,我有後圖片裁剪自動FrameLayout上設置的最後一幀圖像。我們可以從框架佈局中獲得該設置的框架嗎?這怎麼可能?

這裏是像這樣的問題How to retrieve the visible part of a SurfaceView in Android做任何一個有解決辦法。

我想通過這條線ThumbnailUtils.extractThumbnail(source, newWidth, newHeight);來實現這一點,並通過這條線我得到像圖中描述的圖像的src。

什麼改變這一行恰好?

enter image description here

+0

發佈您看到的圖像的快照 – Prem

+0

完成!請檢查更新後的帖子 –

+0

令人難以置信的簡單解決方案:http://stackoverflow.com/a/17733530/294884 – Fattie

回答

0

@Akanksha請使用下面的代碼,你只需要通過保存的圖像文件的路徑,我們的ImageView的HIGHT和寬度。這段代碼適合我。


    import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 

public class ImageHandler { 
    /** 
    * Decode and sample down a bitmap from a file to the requested width and 
    * height. 
    * 
    * @param filename 
    *   The full path of the file to decode 
    * @param reqWidth 
    *   The requested width of the resulting bitmap 
    * @param reqHeight 
    *   The requested height of the resulting bitmap 
    * @return A bitmap sampled down from the original with the same aspect 
    *   ratio and dimensions that are equal to or greater than the 
    *   requested width and height 
    */ 


public static Bitmap decodeSampledBitmapFromFile(String filename, 
      int reqWidth, int reqHeight) { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(filename, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeFile(filename, options); 
    } 



public static int calculateInSampleSize(BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 
      if (width > height) { 
       inSampleSize = Math.round((float) height/(float) reqHeight); 
      } else { 
       inSampleSize = Math.round((float) width/(float) reqWidth); 
      } 

      // This offers some additional logic in case the image has a 
      // strange 
      // aspect ratio. For example, a panorama may have a much larger 
      // width than height. In these cases the total pixels might 
      // still 
      // end up being too large to fit comfortably in memory, so we 
      // should 
      // be more aggressive with sample down the image (=larger 
      // inSampleSize). 

      final float totalPixels = width * height; 

      // Anything more than 2x the requested pixels we'll sample down 
      // further. 
      final float totalReqPixelsCap = reqWidth * reqHeight * 2; 

      while (totalPixels/(inSampleSize * inSampleSize) > totalReqPixelsCap) { 
       inSampleSize++; 
      } 
     } 
     return inSampleSize; 
    } 
} 

我叫異步任務裏面這個方法,因爲它可能需要太多UImemory和時間 這是我如何調用它。


class Asyncing extends AsyncTask { 

     private int reqWidth; 
     private int reqHeight; 
     private ImageView iv; 
     private String fileName; 
     private ProgressDialog pd; 

     public Asyncing(int reqWidth, int reqHeight, ImageView iv, 
       String fileName) { 
      super(); 
      this.reqWidth = reqWidth; 
      this.reqHeight = reqHeight; 
      this.fileName = fileName; 
      this.iv = iv; 
     } 

     @Override 
     protected Bitmap doInBackground(String... params) { 
      return ImageHandler.decodeSampledBitmapFromFile(params[0], 
        reqWidth, reqHeight); 

     } 

     @Override 
     protected void onPostExecute(Bitmap result) { 
      iv.setImageBitmap(result); 
      if (pd.isShowing()) { 
       pd.setMessage(getString(R.string.completed)); 
       pd.dismiss(); 
      } 

      super.onPostExecute(result); 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 

      super.onProgressUpdate(values); 
     } 

     @Override 
     protected void onPreExecute() { 
      pd = ProgressDialog.show(CustomerDetailsActivity.this, "", 
        getString(R.string.processing_signature)); 
      super.onPreExecute(); 
     } 

    } 

這就是你需要調用的AsyncTask


signedImagePath = data.getExtras().getString("imagePath"); 

      new Asyncing(signature_img.getWidth(), signature_img.getHeight(), 
        signature_img, "spenTest.png").execute(signedImagePath); 

上面的代碼是根據我的要求寫的,您可以根據您的修改。

+0

這裏signedImagePath是圖像文件的路徑,spentest.png是圖像文件的名稱。 –

0

中心裁剪圖像可能會幫助你這個。

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) { 
    int sourceWidth = source.getWidth(); 
    int sourceHeight = source.getHeight(); 

    // Compute the scaling factors to fit the new height and width, respectively. 
    // To cover the final image, the final scaling will be the bigger 
    // of these two. 
    float xScale = (float) newWidth/sourceWidth; 
    float yScale = (float) newHeight/sourceHeight; 
    float scale = Math.max(xScale, yScale); 

    // Now get the size of the source bitmap when scaled 
    float scaledWidth = scale * sourceWidth; 
    float scaledHeight = scale * sourceHeight; 

    // Let's find out the upper left coordinates if the scaled bitmap 
    // should be centered in the new size give by the parameters 
    float left = (newWidth - scaledWidth)/2; 
    float top = (newHeight - scaledHeight)/2; 

    // The target rectangle for the new, scaled version of the source bitmap will now 
    // be 
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight); 

    // Finally, we create a new bitmap of the specified size and draw our new, 
    // scaled bitmap onto it. 
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig()); 
    Canvas canvas = new Canvas(dest); 
    canvas.drawBitmap(source, null, targetRect, null); 

    return dest; 
}