android压缩图片代码,Android图片实现压缩处理的实例代码

Android图片实现压缩处理的实例代码

发布时间:2020-10-01 19:26:32

来源:脚本之家

阅读:60

作者:MrZang

整理文档,搜刮出一个Android图片实现压缩处理的实例代码,稍微整理精简一下做下分享。

详解:

1.获取本地图片File文件 获取BitmapFactory.Options对象 计算原始图片 目标图片宽高比 计算输出的图片宽高

2.根据宽高比计算options.inSampleSize值(缩放比例 If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.)得到bitmap位图 根据位图对象获取新的输出位图对象 Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)Creates a new bitmap, scaled from an existing bitmap, whenpossible.

3.获取图片方向调整、失量压缩图片保持在1024kb以下

//进行大小缩放来达到压缩的目的

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

BitmapFactory.decodeFile(srcImagePath, options);

//根据原始图片的宽高比和期望的输出图片的宽高比计算最终输出的图片的宽和高

float srcWidth = options.outWidth;

float srcHeight = options.outHeight;

float maxWidth = outWidth;

float maxHeight = outHeight;

float srcRatio = srcWidth / srcHeight; //原始图片宽高比

float outRatio = maxWidth / maxHeight; //目标图片宽高比

float actualOutWidth = srcWidth;

float actualOutHeight = srcHeight;

if (srcWidth > maxWidth || srcHeight > maxHeight) {

if(srcRatio>outRatio){ //原始宽高比大于目标宽高比

actualOutWidth = maxWidth;

actualOutHeight = actualOutWidth / srcRatio;

}else if(srcRatio

actualOutHeight = maxHeight;

actualOutWidth = actualOutHeight * srcRatio;

}

}else{

actualOutWidth = maxWidth;

actualOutHeight = maxHeight;

}

options.inSampleSize = computSampleSize(options, actualOutWidth, actualOutHeight);

options.inJustDecodeBounds = false;

Bitmap scaledBitmap = null;

try {

scaledBitmap = BitmapFactory.decodeFile(srcImagePath, options);

} catch (OutOfMemoryError e) {

e.printStackTrace();

}

if (scaledBitmap == null) {

return null;

}

//生成最终输出的bitmap

Bitmap actualOutBitmap = Bitmap.createScaledBitmap(scaledBitmap, (int) actualOutWidth, (int) actualOutHeight, true);

//释放原始位图资源

if(scaledBitmap!=actualOutBitmap){ //判断目标位图是否和原始位图指向栈目标相同

scaledBitmap.recycle();

scaledBitmap = null;

}

//处理图片旋转问题

ExifInterface exif = null;

try {

exif = new ExifInterface(srcImagePath);

int orientation = exif.getAttributeInt(

ExifInterface.TAG_ORIENTATION, 0);

Matrix matrix = new Matrix();

if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {

matrix.postRotate(90);

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {

matrix.postRotate(180);

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {

matrix.postRotate(270);

}

actualOutBitmap = Bitmap.createBitmap(actualOutBitmap, 0, 0,

actualOutBitmap.getWidth(), actualOutBitmap.getHeight(), matrix, true);

} catch (IOException e) {

e.printStackTrace();

return null;

}

//进行有损压缩

ByteArrayOutputStream baos = new ByteArrayOutputStream();

int options_ = 100;

actualOutBitmap.compress(Bitmap.CompressFormat.JPEG, options_, baos);//质量压缩方法,把压缩后的数据存放到baos中 (100表示不压缩,0表示压缩到最小)

int baosLength = baos.toByteArray().length;

while (baosLength / 1024 > maxFileSize) {//循环判断如果压缩后图片是否大于maxMemmorrySize,大于继续压缩

baos.reset();//重置baos即让下一次的写入覆盖之前的内容

options_ = Math.max(0, options_ - 10);//图片质量每次减少10

actualOutBitmap.compress(Bitmap.CompressFormat.JPEG, options_, baos);//将压缩后的图片保存到baos中

baosLength = baos.toByteArray().length;

if (options_ == 0)//如果图片的质量已降到最低则,不再进行压缩

break;

}

actualOutBitmap.recycle();

//将bitmap保存到指定路径

FileOutputStream fos = null;

String filePath = getOutputFileName(srcImagePath);

try {

fos = new FileOutputStream(filePath);

//包装缓冲流,提高写入速度

BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fos);

bufferedOutputStream.write(baos.toByteArray());

bufferedOutputStream.flush();

} catch (FileNotFoundException e) {

return null;

} catch (IOException e) {

return null;

} finally {

if (baos != null) {

try {

baos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

//获取位图缩放比例

private int computSampleSize(BitmapFactory.Options options, float reqWidth, float reqHeight) {

float srcWidth = options.outWidth;//20

float srcHeight = options.outHeight;//10

int sampleSize = 1;

if (srcWidth > reqWidth || srcHeight > reqHeight) {

int withRatio = Math.round(srcWidth / reqWidth);

int heightRatio = Math.round(srcHeight / reqHeight);

sampleSize = Math.min(withRatio, heightRatio);

}

return sampleSize;

}

压缩比例换算:

float srcWidth = options.outWidth;

float srcHeight = options.outHeight;

float widthScale = outWidth / srcWidth;//目标/原始 宽比例

float heightScale = outHeight / srcHeight; //目标原始 高比

//对比宽高比选择较大的一种比例

float scale = widthScale > heightScale ? widthScale : heightScale;

float actualOutWidth = srcWidth;

float actualOutHeight = srcHeight;

if (scale < 1) {

actualOutWidth = srcWidth * scale;

actualOutHeight = srcHeight * scale;

}

设置缩放比例--生成新的位图

Matrix matrix1 = new Matrix();

matrix1.postScale(scale, scale);// 放大缩小比例

//生成最终输出的bitmap

Bitmap actualOutBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix1, true);

if (actualOutBitmap != scaledBitmap) {

scaledBitmap.recycle();

scaledBitmap = null;

System.gc();

}

参考:https://github.com/guizhigang/LGImageCompressor

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现OSS(Object Storage Service)批量下载图片压缩的Java代码可以通过以下步骤完成: 1. 引入相关的依赖:在Java的项目中,首先需要在pom.xml或者build.gradle文件中添加OSS SDK的依赖。例如,可以使用阿里云官方提供的Java SDK:aliyun-java-sdk-oss。 2. 配置OSS连接信息:在代码中配置连接OSS所需要的信息,包括Endpoint、Access Key ID、Access Key Secret等。可以在阿里云控制台获取这些信息,具体参考阿里云官方文档。 3. 获取OSS客户端:使用配置好的信息创建OSS客户端实例。 4. 列举Bucket中的文件:通过OSS客户端调用listObjects方法,传入Bucket的名称,获取该Bucket中的所有文件列表。 5. 遍历文件列表并下载:对于每个文件,通过OSS客户端调用getObject方法,传入Bucket的名称和文件的路径,获取文件的输入流,然后将输入流写入本地文件。 6. 图片压缩:使用第三方的图片处理库,如Thumbnails或ImageMagick等,在下载完文件后对图片进行压缩处理。可以通过调整图片的质量、尺寸等参数来实现压缩。 7. 代码示例: ```java import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.GetObjectRequest; import net.coobird.thumbnailator.Thumbnails; import java.io.File; import java.io.InputStream; public class OSSImageDownloader { private static final String ACCESS_KEY_ID = "your-access-key-id"; private static final String ACCESS_KEY_SECRET = "your-access-key-secret"; private static final String ENDPOINT = "your-oss-endpoint"; private static final String BUCKET_NAME = "your-bucket-name"; public static void main(String[] args) { OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET); for (String objectKey : ossClient.listObjects(BUCKET_NAME).getObjectSummaries()) { File localFile = new File("path/to/save/" + objectKey); ossClient.getObject(new GetObjectRequest(BUCKET_NAME, objectKey), localFile); try { InputStream inputStream = localFile.toURI().toURL().openStream(); Thumbnails.of(inputStream) .size(200, 200) .outputQuality(0.8) .toFile(localFile); } catch (Exception e) { e.printStackTrace(); } } ossClient.shutdown(); } } ``` 注意:在代码中需要替换`your-access-key-id`、`your-access-key-secret`、`your-oss-endpoint`和`your-bucket-name`为相应的实际值。另外,需要在项目中导入OSS SDK和图片处理库的相关依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值