图片用Base64 和 字符串相互转换 内存压缩 尺寸压缩 等比例压缩
2016-06-16 09:08
806 查看
图片下载 也可以 用 AsyncTask 见本人其他博客
private Bitmap getBitmap(String uri) {
InputStream is=null;
try {
URL file=new URL(uri);
HttpURLConnection conn=(HttpURLConnection)file.openConnection();
conn.setConnectTimeout(5000);
conn.getContentLength();
Log.i("文件长度", conn.getContent() + "");
if(conn.getResponseCode()==200){
//返回码 200 代表访问成功
is=conn.getInputStream();
bitmap= BitmapFactory.decodeStream(is);
is.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bitmap;
}//转换为 bitmap 放在子线程里面 访问网络 不能放在主线程 会报异常
//图片Base64 转换为字符串 在子线程中可以用 handler sendmessage 发送 在主线程接受 并转换为 bitmap 更改主线程
private String ToString(Bitmap bitmap1){
ByteArrayOutputStream b=new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG,100,b);
byte [] bytes=b.toByteArray();
String bit= Base64.encodeToString(bytes,Base64.DEFAULT);
Log.i("图片字符串",bit);
return bit;
}
//字符串转换为 bitmap
private Bitmap toBitmap(String str){
Bitmap bit=null;
byte[] bitArray;
bitArray=Base64.decode(str,Base64.DEFAULT);
bit=BitmapFactory.decodeByteArray(bitArray,0,bitArray.length);
return bit;
}
//按比例 压缩 图片尺寸 也可以改变图片大小 但是 有具体大小需求的话 不建议采用 有具体大小见下
//按比例压缩图片
private Bitmap changeSize(Bitmap b,int n){
ByteArrayOutputStream out=new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, out);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=n;//此处只能为偶数 奇数无效
ByteArrayInputStream bis=new ByteArrayInputStream(out.toByteArray());
Bitmap bit=BitmapFactory.decodeStream(bis,null,options);
return bit;
}
//按照具体内存大小 修改图片 不失真
private Bitmap changeMapSize(Bitmap bit){
double MaxSize =100.00;//图片允许最大空间
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.JPEG,100,bos);
byte [] b=bos.toByteArray();//字节
//字节转换为 KB
double mid=b.length/1024;//KB
Bitmap bitmap1=null;
if(MaxSize<mid){
//图片超过规定大小
double n=mid/MaxSize; //允许压缩倍数
double newWidth = bit.getWidth() /n;
double newHeight = bit.getHeight() / n;
Matrix matrix=new Matrix();
matrix.postScale(((float)newWidth)/bit.getWidth(),((float)newHeight)/bit.getHeight());
bitmap1=Bitmap.createBitmap(bit,0,0,bit.getWidth(),bit.getHeight(),matrix,true);
}
getSize(bitmap1);
return bitmap1;
}
private void getSize(Bitmap bitmap1) {
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
double ww= byteArrayOutputStream.toByteArray().length/1024;
Log.i("压缩后的大小",ww+"KB");
}
private Bitmap getBitmap(String uri) {
InputStream is=null;
try {
URL file=new URL(uri);
HttpURLConnection conn=(HttpURLConnection)file.openConnection();
conn.setConnectTimeout(5000);
conn.getContentLength();
Log.i("文件长度", conn.getContent() + "");
if(conn.getResponseCode()==200){
//返回码 200 代表访问成功
is=conn.getInputStream();
bitmap= BitmapFactory.decodeStream(is);
is.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bitmap;
}//转换为 bitmap 放在子线程里面 访问网络 不能放在主线程 会报异常
//图片Base64 转换为字符串 在子线程中可以用 handler sendmessage 发送 在主线程接受 并转换为 bitmap 更改主线程
private String ToString(Bitmap bitmap1){
ByteArrayOutputStream b=new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG,100,b);
byte [] bytes=b.toByteArray();
String bit= Base64.encodeToString(bytes,Base64.DEFAULT);
Log.i("图片字符串",bit);
return bit;
}
//字符串转换为 bitmap
private Bitmap toBitmap(String str){
Bitmap bit=null;
byte[] bitArray;
bitArray=Base64.decode(str,Base64.DEFAULT);
bit=BitmapFactory.decodeByteArray(bitArray,0,bitArray.length);
return bit;
}
//按比例 压缩 图片尺寸 也可以改变图片大小 但是 有具体大小需求的话 不建议采用 有具体大小见下
//按比例压缩图片
private Bitmap changeSize(Bitmap b,int n){
ByteArrayOutputStream out=new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, out);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=n;//此处只能为偶数 奇数无效
ByteArrayInputStream bis=new ByteArrayInputStream(out.toByteArray());
Bitmap bit=BitmapFactory.decodeStream(bis,null,options);
return bit;
}
//按照具体内存大小 修改图片 不失真
private Bitmap changeMapSize(Bitmap bit){
double MaxSize =100.00;//图片允许最大空间
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.JPEG,100,bos);
byte [] b=bos.toByteArray();//字节
//字节转换为 KB
double mid=b.length/1024;//KB
Bitmap bitmap1=null;
if(MaxSize<mid){
//图片超过规定大小
double n=mid/MaxSize; //允许压缩倍数
double newWidth = bit.getWidth() /n;
double newHeight = bit.getHeight() / n;
Matrix matrix=new Matrix();
matrix.postScale(((float)newWidth)/bit.getWidth(),((float)newHeight)/bit.getHeight());
bitmap1=Bitmap.createBitmap(bit,0,0,bit.getWidth(),bit.getHeight(),matrix,true);
}
getSize(bitmap1);
return bitmap1;
}
private void getSize(Bitmap bitmap1) {
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
double ww= byteArrayOutputStream.toByteArray().length/1024;
Log.i("压缩后的大小",ww+"KB");
}
相关文章推荐
- Eclipse项目代码和AS项目代码整合开发
- .Net GC 机制原理
- onWindowFocusChanged重要作用
- C# 调用 C++
- SpeechSynthesizer/WaveHeader 存在严重的内存泄漏
- C#游戏框架uFrame
- lnux内核的malloc实现(Oracle的cache buffer影子)
- ASP.NET Core 中文文档 第二章 指南(4.4)添加 Model
- DataGridView如何实现列标头带数据筛选功能,就象Excel高级筛选功能一样
- 词典
- 开篇之作
- C#递归算法之快速排序
- StreamUtil
- 谈一谈CMU导师和学生的互动方式
- 这世界大有搞头
- set容器
- Copying Accelerated Video Decode Frame Buffers
- 第十四周阅读项目(1)
- 10 11周阅读程序(2)
- MFC中使用openCV显示、处理图像