android拍照模糊,解决Android拍照并显示在ImageView中变模糊

本文介绍了一种实用的方法来计算图片缩放比例并实现图片的缩放及保存。通过自定义的Java类,可以有效地调整图片尺寸,并将处理后的图片保存到本地。文章详细展示了如何使用Matrix进行缩放操作以及如何将Bitmap格式的图片转换为JPG文件并存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

public class ImageThumbnail {

public static int reckonThumbnail(int oldWidth, int oldHeight,

int newWidth, int newHeight) {

if ((oldHeight > newHeight && oldWidth > newWidth)

|| (oldHeight <= newHeight && oldWidth > newWidth)) {

int be = (int) (oldWidth / (float) newWidth);

if (be <= 1)

be = 1;

return be;

} else if (oldHeight > newHeight && oldWidth <= newWidth) {

int be = (int) (oldHeight / (float) newHeight);

if (be <= 1)

be = 1;

return be;

}

return 1;

}

public static Bitmap PicZoom(Bitmap bmp, int width, int height) {

int bmpWidth = bmp.getWidth();

int bmpHeght = bmp.getHeight();

Matrix matrix = new Matrix();

matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);

return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);

}

public static String savaPhotoToLocal(Intent data, Bitmap btp) {

// 如果文件夹不存在则创建文件夹,并将bitmap图像文件保存

String filename = System.currentTimeMillis() + ".jpg";

File tempFile = new File(SgsCons.ROOT_DIR, filename);

String filePath = tempFile.getAbsolutePath();

try {

// 将bitmap转为jpg文件保存

FileOutputStream fileOut = new FileOutputStream(tempFile);

btp.compress(CompressFormat.JPEG, 100, fileOut);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

return filePath;

}

}

public class ImageThumbnail {

public static int reckonThumbnail(int oldWidth, int oldHeight,

int newWidth, int newHeight) {

if ((oldHeight > newHeight && oldWidth > newWidth)

|| (oldHeight <= newHeight && oldWidth > newWidth)) {

int be = (int) (oldWidth / (float) newWidth);

if (be <= 1)

be = 1;

return be;

} else if (oldHeight > newHeight && oldWidth <= newWidth) {

int be = (int) (oldHeight / (float) newHeight);

if (be <= 1)

be = 1;

return be;

}

return 1;

}

public static Bitmap PicZoom(Bitmap bmp, int width, int height) {

int bmpWidth = bmp.getWidth();

int bmpHeght = bmp.getHeight();

Matrix matrix = new Matrix();

matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);

return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);

}

public static String savaPhotoToLocal(Intent data, Bitmap btp) {

// 如果文件夹不存在则创建文件夹,并将bitmap图像文件保存

String filename = System.currentTimeMillis() + ".jpg";

File tempFile = new File(SgsCons.ROOT_DIR, filename);

String filePath = tempFile.getAbsolutePath();

try {

// 将bitmap转为jpg文件保存

FileOutputStream fileOut = new FileOutputStream(tempFile);

btp.compress(CompressFormat.JPEG, 100, fileOut);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

return filePath;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值