[英]How to set the captured images using camera to fit into a Imageview of fixed height and width
Hi i have followed android developer site and tried to implement capture an image from camera programatically. 嗨,我已经关注了android开发者网站,并尝试以编程方式实现从相机捕获图像。 I am able to capture the image and set it in the ImageView. 我能够捕获图像并将其设置在ImageView中。
But when I am setting the image into the ImageView i am getting an image of less width and height. 但是,当我将图像设置为ImageView时,我得到的图像的宽度和高度都较小。 Instead i want the image captured from gallery or camera should fit into the ImageView element layout. 相反,我希望从图库或照相机中捕获的图像应适合ImageView元素布局。
MyXML file is like as follows: 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"
/>
Since i have given the width as match_parent which is full screen I am getting less width. 由于我给定的宽度是match_parent,这是全屏显示,所以我得到的宽度越来越小。 My requirement is like it should fit the ImageView layout. 我的要求是,它应该适合ImageView布局。 For camera coding I have followed this url: 对于相机编码,我遵循以下网址:
http://developer.android.com/training/camera/photobasics.html http://developer.android.com/training/camera/photobasics.html
Maybe you can try imgview.setScaleType(ScaleType.FIT_XY);
也许您可以尝试imgview.setScaleType(ScaleType.FIT_XY);
From XML, use this syntax: android:scaleType="fitXY"
. 从XML使用以下语法: android:scaleType="fitXY"
。
The image is scaled using Matrix.ScaleToFit FILL
, which performs the following: 使用Matrix.ScaleToFit FILL
缩放图像,该图像执行以下操作:
Scale in X and Y independently, so that src matches dst exactly. 分别缩放X和Y,以便src与dst完全匹配。 This may change the aspect ratio of the src. 这可能会更改src的纵横比。
See Android Documentation . 请参阅Android文档 。
Try this, 尝试这个,
android:scaleType="fitXY"
to your imageview inside xml file. 到xml文件中的imageview。
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"
/>
Use this method to fit ur image and get rounded corner also 使用此方法适合您的图像并获得圆角
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;
}
And Set image to ImageView 并将图像设置为ImageView
addimg.setImageBitmap(getRoundedCornerBitmap(docode("Give path of your image here")));
Save image in sd card or phone memory, Decode it using its path. 将图像保存在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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.