2014-09-26 61 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; 
} 
相关问题