图片/Bitmap工具类
1、根据uri解码图片,通常用在从相册选择照片
(1)此方法包含了压缩Bitmap,根据目标尺寸缩放等
/**
* 根据Uri解码图片
*
* @param selectedImage 图片的Uri
* @return 解码后的Bitmap对象
* @throws FileNotFoundException 如果文件找不到,则抛出此异常
*/
public static Bitmap decodeUri(Context context, Uri selectedImage) throws FileNotFoundException
{
// 解码图片尺寸
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o);
// 设置目标尺寸
final int REQUIRED_SIZE = 400;
// 计算正确的缩放比例,应该是2的幂次方
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 *= 2;
}
// 按照缩放比例解码图片
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o2);
// 压缩Bitmap以确保其大小不超过1MB
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
while (baos.toByteArray().length > 1024 * 1024 && quality > 10) {
baos.reset();
quality -= 10;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
}
// 回收原始Bitmap以释放内存
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}
// 返回压缩后的Bitmap
return BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length);
}
(2)此方法为简单的根据uri解码图片
public static Bitmap getBitmapFromUri(Context context, Uri uri) throws IOException {
ContentResolver contentResolver = context.getContentResolver();
InputStream inputStream = contentResolver.openInputStream(uri);
return BitmapFactory.decodeStream(inputStream);
}
2、将图片的Bitmap转换为byte类型
public byte[] bitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
3、将图片保存在文件中并返回地址(可用在不同的Activity中传递图片信息,详情请参考:写文章-CSDN创作中心)
public static File saveBitmapToFile(Context context, Bitmap bitmap) {
File imageFile = new File(context.getExternalCacheDir(), "selected_image.jpg");
try (FileOutputStream fos = new FileOutputStream(imageFile)) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
return imageFile;
} catch (IOException e) {
Log.e(TAG, "Failed to save bitmap to file: " + e.getMessage());
}
return null;
}
4、将Bitmap转换为Base64字符串
/**
* 将Bitmap转换为Base64字符串
* @param bitmap 要转换的Bitmap对象
* @return 转换后的Base64字符串
*/
public static String bitmapToBase64(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
5、将Bitmap转换为Base64字符串并进行URL编码(需用到上面的方法)
/**
* 将Bitmap转换为Base64字符串,并进行URL编码
* @param bitmap 要转换的Bitmap对象
* @return 转换后进行URL编码的字符串
*/
public static String encodeAndPrepareImage(Bitmap bitmap) {
String base64String = bitmapToBase64(bitmap);
// 使用兼容的方式进行URL编码
String encodedString = "";
try {
encodedString = URLEncoder.encode(base64String, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
// 处理异常情况
e.printStackTrace();
}
return encodedString;
}
标签:return,安卓,bitmap,BitmapFactory,context,new,Android,Bitmap
From: https://blog.csdn.net/m0_54198552/article/details/143363757