[英]How can i decrease the width and height for a image in android pragmatically?
My requirement is what ever the size(width,height not file size) image stored in the database i need to retrieving that as simple passport size of image. 我的要求是存储在数据库中的图像大小(宽度,高度而不是文件大小)是什么,我需要将其检索为图像的简单护照大小。
My code is : 我的代码是:
byte[] imgByte=null;
in the oncreate method 在oncreate方法中
imageview=new ImageView(this);
imageview.setLayoutParams(llp2);
imgByte=cursor.getBlob(cursor.getColumnIndex("imagestore"));
imageview.setScaleType(ScaleType.CENTER);
imageview.setImageBitmap(BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length));
layout.addView(imageview)
when i displaying that displayed only what ever the size before enter. 当我显示时,只显示输入之前的尺寸。 so i need to fit that image size in the android.
所以我需要在Android中适应该图像大小。
I tried these links 我尝试了这些链接
Reduce size of Bitmap to some specified pixel in Android 在Android中将位图的大小减小到某些指定像素
but the problem here is i got image in byte. 但是这里的问题是我得到了字节图像。 But all codes image data type int only.
但是所有代码图像数据类型仅是int。 can i get the correct solution to decrease the image size pragmatically?
我可以找到正确的解决方案以实用地减小图像尺寸吗?
You can use following code to create sample sized image. 您可以使用以下代码创建样本大小的图像。 This will return
Bitmap
. 这将返回
Bitmap
。
public static Bitmap decodeSampledBitmapFromResource(Resources res,
int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, 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) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
Use this code, 使用此代码,
decodeSampledBitmapFromResource(getResources(),R.drawable.xyz, 100, 100);
Here, 100 * 100 sample size you are providing. 在这里,您提供的样本大小为100 * 100。 So Thumbnail of such size will be created.
因此将创建这种大小的缩略图。
Edit 编辑
To use bytes[] in code, use following short of code. 要在代码中使用bytes [],请使用以下简短代码。
public static Bitmap decodeSampledBitmap(byte[] data, int reqWidth,
int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.