更改 Bitmap 分辨率
private Bitmap SetBmpResolution(Bitmap nBmp, int nWidth, int nHeight) {
// 原图大小
int width = nBmp.getWidth();
int height = nBmp.getHeight();
// 计算缩放比例
float scaleWidth = ((float) nWidth) / width;
float scaleHeight = ((float) nHeight) / height;
// 取得想要缩放的matrix参数 android.graphics.Matrix;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
return Bitmap.createBitmap(nBmp, 0, 0, width, height, matrix, true);
}
Bitmap 旋转角度
Bitmap bitmap = BitmapFactory.decodeByteArray(imgdata, 0,imgdata.length); Matrix matrix = new Matrix(); matrix.preRotate(270); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bmbitmapgetHeight(), matrix, true);
Bitmap 保存为PNG格式的文件
private void BmpToSavePNG(Bitmap nBmp, String nFilePath) {
try {
File lFile = new File(nFilePath);
if (lFile.exists()) { // 如果文件存在
if (!lFile.delete()) {
Log.d("Test","Delete file failed!");
}
}
//文件输出流
FileOutputStream out = new FileOutputStream(nFilePath);
//压缩图片,png:Bitmap.CompressFormat.PNG,jpg:Bitmap.CompressFormat.JPEG,质量100%,表示不压缩
nBmp.compress(Bitmap.CompressFormat.PNG, 100, out);
//写入
out.flush();
//关闭流
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
本文详细介绍了如何使用Android平台的Bitmap类进行图像分辨率调整、旋转及格式转换。包括计算缩放比例并应用到Matrix参数,实现图像的等比缩放;通过Matrix预旋转功能,改变Bitmap的角度;以及将Bitmap保存为PNG格式的文件。

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



