Nul*_*ion 314 base64 android bitmap
我从远程数据库中获取了一个Base64字符串的位图,(encodedImage
是用Base64表示图像的字符串):
profileImage = (ImageView)findViewById(R.id.profileImage);
byte[] imageAsBytes=null;
try {
imageAsBytes = Base64.decode(encodedImage.getBytes());
} catch (IOException e) {e.printStackTrace();}
profileImage.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
Run Code Online (Sandbox Code Playgroud)
profileImage是我的ImageView
好的,但是我必须在显示我ImageView
的布局之前调整此图像的大小.我必须将其调整为120x120.
有人能告诉我调整大小的代码吗?
我找到的示例无法应用于获得位图的base64字符串.
use*_*209 516
更改:
profileImage.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
Run Code Online (Sandbox Code Playgroud)
至:
Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));
Run Code Online (Sandbox Code Playgroud)
jee*_*wat 271
import android.graphics.Matrix
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
bm.recycle();
return resizedBitmap;
}
Run Code Online (Sandbox Code Playgroud)
编辑:正如@aveschini所建议的那样,我添加bm.recycle();
了内存泄漏.请注意,如果您将上一个对象用于其他目的,请相应处理.
Zen*_*nce 113
如果您已有位图,则可以使用以下代码调整大小:
Bitmap originalBitmap = <original initialization>;
Bitmap resizedBitmap = Bitmap.createScaledBitmap(
originalBitmap, newWidth, newHeight, false);
Run Code Online (Sandbox Code Playgroud)
sag*_*its 35
基于宽高比的比例:
float aspectRatio = yourSelectedImage.getWidth() /
(float) yourSelectedImage.getHeight();
int width = 480;
int height = Math.round(width / aspectRatio);
yourSelectedImage = Bitmap.createScaledBitmap(
yourSelectedImage, width, height, false);
Run Code Online (Sandbox Code Playgroud)
使用height作为宽度的基本intead更改为:
int height = 480;
int width = Math.round(height * aspectRatio);
Run Code Online (Sandbox Code Playgroud)
Kev*_*vin 22
使用目标最大大小和宽度缩放位图,同时保持纵横比:
int maxHeight = 2000;
int maxWidth = 2000;
float scale = Math.min(((float)maxHeight / bitmap.getWidth()), ((float)maxWidth / bitmap.getHeight()));
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
Run Code Online (Sandbox Code Playgroud)
试试这个代码:
BitmapDrawable drawable = (BitmapDrawable) imgview.getDrawable();
Bitmap bmp = drawable.getBitmap();
Bitmap b = Bitmap.createScaledBitmap(bmp, 120, 120, false);
Run Code Online (Sandbox Code Playgroud)
我希望它有用.
在这种情况下有人问如何保持纵横比:
计算用于缩放的因子,并将其用于两个维度.假设您希望图像高度为屏幕的20%
int scaleToUse = 20; // this will be our percentage
Bitmap bmp = BitmapFactory.decodeResource(
context.getResources(), R.drawable.mypng);
int sizeY = screenResolution.y * scaleToUse / 100;
int sizeX = bmp.getWidth() * sizeY / bmp.getHeight();
Bitmap scaled = Bitmap.createScaledBitmap(bmp, sizeX, sizeY, false);
Run Code Online (Sandbox Code Playgroud)
要获得屏幕分辨率,您可以使用此解决方案: 以像素为单位获取屏幕尺寸
尽管接受的答案是正确的,但它不会Bitmap
通过保持相同的Aspect Ratio来调整大小。如果您正在寻找一种Bitmap
通过保持相同纵横比来调整大小的方法,您可以使用以下实用函数。此链接提供了该功能的使用详细信息和说明。
public static Bitmap resizeBitmap(Bitmap source, int maxLength) {
try {
if (source.getHeight() >= source.getWidth()) {
int targetHeight = maxLength;
if (source.getHeight() <= targetHeight) { // if image already smaller than the required height
return source;
}
double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
int targetWidth = (int) (targetHeight * aspectRatio);
Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
if (result != source) {
}
return result;
} else {
int targetWidth = maxLength;
if (source.getWidth() <= targetWidth) { // if image already smaller than the required height
return source;
}
double aspectRatio = ((double) source.getHeight()) / ((double) source.getWidth());
int targetHeight = (int) (targetWidth * aspectRatio);
Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
if (result != source) {
}
return result;
}
}
catch (Exception e)
{
return source;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
406338 次 |
最近记录: |