图片/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中传递图片信息,详情请参考:安卓Android 使用Intent在Activity之间传递图片_android intent 传递bitmap-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;
}
——新增——
6、将 assets 文件夹中的图片转换为 Bitmap
/**
* 将 assets 文件夹中的图片转换为 Bitmap
* @param context 应用上下文
* @param assetPath assets 文件夹中的图片路径
* @return 转换后的 Bitmap 对象
*/
public static Bitmap assetToBitmap(Context context, String assetPath) {
try {
// 获取 AssetManager
AssetManager assetManager = context.getAssets();
// 打开文件输入流
InputStream inputStream = assetManager.open(assetPath);
// 使用 BitmapFactory 从输入流中读取图片
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// 关闭输入流
inputStream.close();
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
7、将 drawable 资源转换为 Bitmap
/**
* 将 drawable 资源转换为 Bitmap
* @param context 应用上下文
* @param resourceId drawable 资源的 ID
* @return 转换后的 Bitmap 对象
*/
public static Bitmap drawableToBitmap(Context context, int resourceId) {
// 通过 Context 获取 Resources 对象
Drawable drawable = ContextCompat.getDrawable(context, resourceId);
if (drawable == null) {
return null;
}
// 确保 Drawable 已经绑定到窗口
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
// 创建一个与 Drawable 大小相同的 Bitmap
int intrinsicWidth = drawable.getIntrinsicWidth();
int intrinsicHeight = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888);
// 将 Drawable 绘制到 Bitmap 上
drawable.setBounds(0, 0, intrinsicWidth, intrinsicHeight);
drawable.draw(new Canvas(bitmap));
return bitmap;
}
——新增——
8、Bitmap转ByteArrayInputStream
/**
* Bitmap转ByteArrayInputStream
*
* @param bitmap Bitmap
* @param compressFormat 压缩格式
* @param quality 质量等级 压缩质量等级100意味着不会有任何损失,但这会增加输出流的大小
* @return ByteArrayInputStream
*/
public static ByteArrayInputStream bitmapToInputStream(Bitmap bitmap,
Bitmap.CompressFormat compressFormat, int quality) {
if (bitmap == null) {
return null;
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(compressFormat, quality, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return new ByteArrayInputStream(byteArray);
}

418

被折叠的 条评论
为什么被折叠?



