介绍多张图片压缩转Bas64速度优化问题
来源:互联网 发布:关于走心的文案 知乎 编辑:程序博客网 时间:2024/06/05 16:22
今天项目有一个需求,要求多张图片上传,并且时间在7秒左右,废话不多说上效果图
我解决的方案有二种:
- 第一种是用线程的方式去上传图片,结果发现同时压缩4M的图非常的慢,以及中途转Bas64用时20多秒,当然这根据机型而定,代码非常的简单我还是贴出来:
StringBuffer stringBuffer = new StringBuffer(); for (int i = 0; i < list.size(); i++) { Bitmap bitmap = getBitmap(list.get(i)); String path = SaveBitmap(bitmap, i);// fileList.add(path); Bitmap bitmapNew = ImageUtil.getLocalBitmap(path); String photoString = NativeUtil.getcomImageBase64(bitmapNew); stringBuffer.append(photoString).append(","); } String mIamgeString = stringBuffer.toString().substring(0, stringBuffer.length() - 1);`
2.第二种方式是用AsyncTask,我直接贴代码了:
package com.sdy.zhuanqianbao.kit;import android.app.ProgressDialog;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.os.Environment;import java.io.File;import java.io.FileOutputStream;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;/** * Created by csl on 2017/8/17. */public class CompressPhotoUtils { private List<String> fileList = new ArrayList<>(); private ProgressDialog progressDialog; private static File file; public void CompressPhoto(Context context, List<String> list, CompressCallBack callBack) { CompressTask task = new CompressTask(context, list, callBack); task.execute(); } class CompressTask extends AsyncTask<Void, Integer, String> { private Context context; private List<String> list; private CompressCallBack callBack; CompressTask(Context context, List<String> list, CompressCallBack callBack) { this.context = context; this.list = list; this.callBack = callBack; if (DeviceUtils.isExitsSdcard()) { String filePath = Environment.getExternalStorageDirectory() + "/huizhuan/upload"; file = new File(filePath); } } /** * 运行在UI线程中,在调用doInBackground()之前执行 */ @Override protected void onPreExecute() {// progressDialog = ProgressDialog.show(context, null, "处理中..."); } /** * 后台运行的方法,可以运行非UI线程,可以执行耗时的方法 */ @Override protected String doInBackground(Void... params) { StringBuffer stringBuffer = new StringBuffer(); for (int i = 0; i < list.size(); i++) { Bitmap bitmap = getBitmap(list.get(i)); String path = SaveBitmap(bitmap, i);// fileList.add(path); Bitmap bitmapNew = ImageUtil.getLocalBitmap(path); String photoString = NativeUtil.getcomImageBase64(bitmapNew); stringBuffer.append(photoString).append(","); } String mIamgeString = stringBuffer.toString().substring(0, stringBuffer.length() - 1); if (file.exists()){ FileUtils.delete(file); } return mIamgeString; } /** * 运行在ui线程中,在doInBackground()执行完毕后执行 */ @Override protected void onPostExecute(String imageString) {// progressDialog.dismiss(); callBack.success(imageString); } /** * 在publishProgress()被调用以后执行,publishProgress()用于更新进度 */ @Override protected void onProgressUpdate(Integer... values) { } } /** * 从sd卡获取压缩图片bitmap */ public static Bitmap getBitmap(String srcPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; float hh = 800f; float ww = 480f; // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;// be=1表示不缩放 if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); } newOpts.inSampleSize = be;// 设置缩放比例 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return bitmap; } /** * 保存bitmap到内存卡 */ public static String SaveBitmap(Bitmap bmp, int num) { String path = null; if (!file.exists()){ file.mkdirs(); } try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String picName = formatter.format(new Date()); path = file.getPath() + "/" + picName + "-" + num + ".jpg"; FileOutputStream fileOutputStream = new FileOutputStream(path); bmp.compress(Bitmap.CompressFormat.JPEG, 50, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } return path; } public interface CompressCallBack { void success(String imageString); }}
然后Activity里面主要用:
//图片转base64 if (listImgPath.size() != 0) { new CompressPhotoUtils().CompressPhoto(MarketFeedBackActivity.this, listImgPath, new CompressPhotoUtils.CompressCallBack() { @Override public void success(String imageString) { upload(imageString); //执行上传的方法 } }); }
这样就完成了,从20多秒变成5-6秒就成功上传图片了,从数据库里面成功看到上传完成以后数据库
作者:工程师丶佛爷
有更好的优化方案欢迎留言,谢谢.
阅读全文
1 0
- 介绍多张图片压缩转Bas64速度优化问题
- 压缩并用 Alamofire 上传多张图片
- 上传多张图片并压缩
- 图片压缩优化能有效提高网站浏览速度
- 网页图片压缩优化,提高网站打开速度
- android递归压缩上传多张图片到七牛
- js显示多张图片的问题
- 多张图片并列显示的问题
- IOS 处理多张图片内存问题
- 多张图片转pdf
- 在线的图片、js、css压缩优化工具介绍
- 使用多张图片做帧动画的性能优化
- 使用多张图片做帧动画的性能优化
- android 图片压缩优化
- 压缩多张图片,解压后发现图片文件损坏的问题解决
- 关于S3C2440的LCD只能显示2张图片,不能显示多张图片的问题
- 多张图片上传
- 多张图片选择
- Oracle-dual表的作用
- Ubuntu 串口权限问题
- Linux系统编程--终端和守护进程
- CI分页
- 1001: Sum Problem
- 介绍多张图片压缩转Bas64速度优化问题
- ssm整合
- Demo(动态数组的添加输出子类继承的排序反转)
- CI AR模型
- CodeIgniter框架源码学习之安全类--Security.php
- python代码简单实现一个词频统计
- Window下安装配置PyOpenGL
- 2017多校训练第10场-Schedule(贪心+二分)
- linux学习笔记(9)