Bitmap之compress图片压缩
package com.loaderman.customviewdemo; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ImageView iv_1 = (ImageView) findViewById(R.id.img1);
ImageView iv_2 = (ImageView) findViewById(R.id.img2); Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.cat);
iv_1.setImageBitmap(bmp); //压缩图像后,显示
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 1, bos);
byte[] bytes = bos.toByteArray();
Bitmap bmp1 = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
iv_2.setImageBitmap(bmp1); ImageView iv_3 = (ImageView) findViewById(R.id.img3);
ImageView iv_4 = (ImageView) findViewById(R.id.img4); ByteArrayOutputStream bos3 = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 1, bos3); byte[] bytes3 = bos3.toByteArray();
Bitmap bmp3 = BitmapFactory.decodeByteArray(bytes3, 0, bytes3.length); iv_3.setImageBitmap(bmp3); ByteArrayOutputStream bos4 = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.WEBP, 1, bos4); byte[] bytes4 = bos4.toByteArray();
Bitmap bmp4 = BitmapFactory.decodeByteArray(bytes4, 0, bytes4.length); iv_4.setImageBitmap(bmp4); } /**
* 保存文件到手机SD卡根目录中
* @param bitmap
*/
private void saveBmp(Bitmap bitmap) {
File fileDir = Environment.getExternalStorageDirectory();
String path = fileDir.getAbsolutePath() + "/lavor.webp"; File file = new File(path);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.WEBP, 10, outputStream);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
效果:

Bitmap之compress图片压缩的更多相关文章
- 有关Option.inSamplSize 和 Compress 图片压缩
1.图片占用内存:占用的内存 = 图片长度 * 图片宽度 * 单位像素占用的字节数 注: 图片长度和图片宽度就是图片在行列上的像素数量. 图片格式: ALPHA_8:表示8位Alpha位图,即A=8, ...
- Android 图片压缩各种方式
前言:由于公司项目当中需要用到压缩这块的相应技术,之前也做过的图片压缩都不是特别的理想, 所以这次花了很多心思,仔细研究和在网上找到了很多相对应的资料.为了就是 以后再做的时候直接拿来用就可以了 ...
- [Android算法] bitmap 将图片压缩到指定的大小
Bitmap压缩到指定大小: private void imageZoom() {//图片允许最大空间 单位:KBdouble maxSize =400.00;//将bitmap放至数组中,意在bit ...
- 图片处理之-Bitmap.Config,jpeg压缩与大小
关于ARGB_8888.ALPHA_8.ARGB_4444.RGB_565的理解 A:透明度 R:红色 G:绿 B:蓝 Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4 ...
- android 图片压缩
引用:http://104zz.iteye.com/blog/1694762 第一:我们先看下质量压缩方法: private Bitmap compressImage(Bitmap image) { ...
- Bitmap 的几种压缩方式
微信的缩略图要求是不大于32k,这就需要对我的图片进行压缩.试了几种方法,一一道来. 代码如下 ByteArrayOutputStream baos = new ByteArrayOutputSt ...
- (转)android图片压缩总结
原文地址:http://blog.csdn.net/cherry609195946/article/details/9264409 一.图片的存在形式 1.文件形式(即以二进制形式存在于硬盘上)2.流 ...
- Android图片压缩方法总结
本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 第一:质量压缩方法: ? 1 2 3 ...
- Android webview实现上传图片的效果(图片压缩)
mainactivity代码 package com.bwie.webviewupload; import java.io.ByteArrayInputStream; import java.io.B ...
随机推荐
- kubernetes---ConfigMap管理应用配置
1.拆分环境 主机名 角色 ip hdss7-11.host.com zk1.od.com(Test环境) 10.4.7.11 hdss7-12.host.com zk2.od.com(Prod环境) ...
- PAT基础级-钻石段位样卷2-7-6 帅到没朋友 (20 分)
当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友.本题就要求你找出那些帅到没有朋友的人. 输入格式: 输入第一行给出一个正整数N(≤),是已知朋友圈的个数:随后N行,每行首先给出一个 ...
- springboot中用来进行查看错误日志的logback文件
<?xml version="1.0" encoding="UTF-8"?> <!-- 从高到地低 OFF . FATAL . ERROR . ...
- machine learning(13) -- solving the problem of overfitting:regularization
solving the problem of overfitting:regularization 发生的在linear regression上面的overfitting问题 发生在logistic ...
- HTML5 服务器发送事件(Server-Sent Events)
沈阳SEO:HTML5 服务器发送事件(server-sent event)允许网页获得来自服务器的更新. Server-Sent 事件 - 单向消息传递 Server-Sent 事件指的是网页自动获 ...
- 入门: 在Windows上搭建Flutter开发环境
使用镜像 由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时镜像,大家可以将如下环境变量加入到用户环境变量中: export PUB_HOSTED_URL=htt ...
- js中的数据类型以及转换
Js中的数据类型 Js中的数据类型一共有六种,即number,string,boolean,underfine,null,object. 一,number Number数据类型指的是数字,可以为整型, ...
- 原生JS实现九宫格拼图
实现这个案例,需要考虑到鼠标的拖拽效果(onmousedown/onmousemove/mouseup) 拖拽分解: 按下鼠标---->移动鼠标----->松开鼠标 1.给目标元素添加on ...
- sql server select 1 from 的作用
select 1 from table 语句中的1代表什么意思 在这里我主要讨论的有以下几个select 语句: doo_archive表是一个数据表,表的行数为4行,如下: 分别用三条selec ...
- 学习。NET三周心得
目前为止 学习.NET已经快一个月了,有刚开始的不懂,到中途懵懂.再到现在的简懂 ,感觉自己迷了好多天,学习程序员跟学其他的程序还不同,其他的有固定格式,而.NET则固定很少 ,一直在用方法连接前后台 ...