[英]How to load image file with BitmapFactory but not in it's original size (downscale it)
所以我正在使用这段代码
Bitmap myBitmap = BitmapFactory.decodeFile(fname);
加载图像文件。 但有时加载的图像可能像 512x512 一样大,我现在不想加载图像的所有 262144 像素,而是按比例加载它。 我该怎么做?
像这样使用 Bitmap.Compress() :
Uri uri = Uri.fromFile(file);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, QUALITY, stream);
质量论点:
int:压缩机提示,0-100。 0 表示压缩为小尺寸,100 表示压缩为最大质量。 某些格式,例如无损的 PNG,将忽略质量设置
您使用Bitmap myBitmap = BitmapFactory.decodeFile(fname)
获得原始图像。
Integer scaleValue;
Integer scaleHeight = desiredMaxPixelHeight/myBitmap.getHeight();
Integer scaleWidth = desiredMaxPixelWidth/myBitmap.getWidth();
if (scaleHeight < 1 && scaleWidth < 1) scaleValue = 1;
else if (scaleHeight > scaleWidth) scaleValue = scaleHeight;
else scaleValue = scaleWidth;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, myBitmap.getHeight()/scaleValue, myBitmap.getWidth()/scaleValue, false);
缩放逻辑可以做得更好,但重点是您可以检查有关原始 bitmap 的信息,然后使用Bitmap.createScaledBitmap(oldBitmap,sizeX,sizeY,false)
创建新的缩放逻辑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.