2014-09-26 37 views
0

嗨,我遵循android開發人員網站,並試圖實現從攝像頭以編程方式捕獲圖像。我能夠捕捉圖像並將其設置在ImageView中。
但是,當我將圖像設置到ImageView中時,我得到的寬度和高度較小。相反,我希望從圖庫或相機捕獲的圖像應該適合ImageView元素佈局。

enter image description here如何使用相機設置捕獲的圖像,以適應固定的高度和寬度的Imageview

MyXML文件就像如下:

<ImageView 
      android:id="@+id/cmp_camera" 
      android:layout_height="200dp" 
      android:layout_width="match_parent" 
      android:layout_weight="1" 
      android:layout_below="@+id/cmp_title" 
      android:onClick="openCameraDialog" 
      /> 

,因爲我已經給出了寬度match_parent這是全屏幕,我越來越少寬度。 我的要求是它應該適合ImageView佈局。 對於相機編碼我按照這個網址:
http://developer.android.com/training/camera/photobasics.html

+1

使用'scaleType = 「fitXY」' – Piyush 2014-09-26 09:58:19

+1

image1.setScaleType(ScaleType.FIT_XY); – iffu 2014-09-26 10:06:08

+0

[相機顯示/全屏預覽不能保持寬高比 - 圖像偏斜,拉伸以適合屏幕]可能的重複(http://stackoverflow.com/questions/16727836/camera-display-preview-在全屏此結果未維持縱橫比圖像-i)的 – Gattsu 2014-09-26 10:09:52

回答

2

試試這個,

android:scaleType="fitXY" 

你imageview的XML文件中。

2

也許你可以試試imgview.setScaleType(ScaleType.FIT_XY);

從XML,使用此語法:android:scaleType="fitXY"

該圖像是使用Matrix.ScaleToFit FILL縮放,其執行以下操作:在X和Y獨立地

量表,使得SRC完全匹配DST。這可能會改變src的寬高比。

參見Android Documentation

1
ImageView.setImageBitmap(bitmap); 


<ImageView 
     android:id="@+id/cmp_camera" 
     android:layout_height="200dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_below="@+id/cmp_title" 
     android:onClick="openCameraDialog" 
     android:scaleType="fitXY" 
     /> 
0

使用此方法,以適應UR圖像,並獲得圓潤的邊角也

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { 
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 
      bitmap.getHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int color = 0xff424242; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    final RectF rectF = new RectF(rect); 
    final float roundPx = 19; 

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 

    return output; 
} 

,並設置圖像的ImageView

addimg.setImageBitmap(getRoundedCornerBitmap(docode("Give path of your image here"))); 
在SD卡或手機內存

保存圖像,使用其解碼它的路徑。

public static Bitmap decodeFile(File f) { 
    try { 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f), null, o); 

     // Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE = 70; 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE 
        || height_tmp/2 < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale++; 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) { 
     Log.e("decodeFile", "" + e); 
    } 

    return null; 
} 
相關問題