Android 如何使用相机将捕获的图像设置为适合固定高度和宽度的Imageview
您好,我一直在关注android开发者网站,并试图通过编程实现从摄像头捕获图像。我能够捕获图像并将其设置在ImageView中。Android 如何使用相机将捕获的图像设置为适合固定高度和宽度的Imageview,android,android-camera,Android,Android Camera,您好,我一直在关注android开发者网站,并试图通过编程实现从摄像头捕获图像。我能够捕获图像并将其设置在ImageView中。 但是当我将图像设置到ImageView中时,我得到的图像的宽度和高度都较小。相反,我希望从gallery或camera捕获的图像应该适合ImageView元素布局 MyXML文件如下所示: <ImageView android:id="@+id/cmp_camera" android:layout_heigh
但是当我将图像设置到ImageView中时,我得到的图像的宽度和高度都较小。相反,我希望从gallery或camera捕获的图像应该适合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"
/>
因为我已经给了宽度作为匹配_的父母,这是全屏,我得到了较少的宽度。
我的要求是它应该适合ImageView布局。
对于摄像机编码,我遵循以下url:
也许您可以尝试
imgview.setScaleType(ScaleType.FIT_XY)代码>
在XML中,使用以下语法:android:scaleType=“fitXY”
使用Matrix.ScaleToFit FILL
缩放图像,执行以下操作:
在X和Y方向独立缩放,以便src与dst精确匹配。这可能会改变src的纵横比
请参阅。也许您可以尝试imgview.setScaleType(ScaleType.FIT_XY)代码>
在XML中,使用以下语法:android:scaleType=“fitXY”
使用Matrix.ScaleToFit FILL
缩放图像,执行以下操作:
在X和Y方向独立缩放,以便src与dst精确匹配。这可能会改变src的纵横比
请参阅。试试这个
android:scaleType="fitXY"
到您的xml文件内的imageview。试试这个
android:scaleType="fitXY"
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"
/>
到xml文件内的imageview。imageview.setImageBitmap(位图);
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"
/>
ImageView.setImageBitmap(位图);
使用此方法来拟合您的图像并获得圆角
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;
}
公共静态位图解码文件(文件f){
试一试{
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//找到正确的刻度值。它应该是2的幂。
所需的最终int_尺寸=70;
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp/2<要求的\u尺寸
||高度(tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
scale++;
}
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
返回BitmapFactory.decodeStream(新文件输入流(f),null,o2);
}catch(filenotfounde异常){
Log.e(“decodeFile”,即“+e”);
}
返回null;
}
使用此方法来拟合您的图像并获得圆角
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;
}
公共静态位图解码文件(文件f){
试一试{
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//找到正确的刻度值。它应该是2的幂。
所需的最终int_尺寸=70;
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp/2<要求的\u尺寸
||高度(tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
scale++;
}
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
返回BitmapFactory.decodeStream(新文件输入流(f),null,o2);
}catch(filenotfounde异常){
Log.e(“decodeFile”,即“+e”);
}
返回null;
}
使用scaleType=“fitXY”
image1.setScaleType(scaleType.FIT_XY);可能重复使用scaleType=“fitXY”
image1.setScaleType(scaleType.FIT_XY);可能重复的