Bitmap 转字节流字符, 压缩等处理

摘要:
//压缩后,将字节字符publicStringbitmap转换为字符串(Bitmapbitmap){//将位图转换为字符串Stringstring=null;ByteArrayOutputStreambaos=newByteArrayOutputStream();Bitmap.compress(Bitmap.CompressFormat.JPEG,100,bao);//质量压缩方法,其

//压缩后转字节字符
public String bitmaptoString(Bitmap bitmap) {
// 将Bitmap转换成字符串

String string = null;

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while ( baos.toByteArray().length / 1024>100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset();//重置baos即清空baos
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
options -= 10;//每次都减少10
}
byte[] bytes = baos.toByteArray();

string = Base64.encode(bytes);

return string;
}

// 将字符串转换成Bitmap类型

public Bitmap stringtoBitmap(String string) {

Bitmap bitmap = null;

try {

byte[] bitmapArray;

bitmapArray = Base64.decode(string);

bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,

bitmapArray.length);

} catch (Exception e) {

e.printStackTrace();

}
return bitmap;

}
//图片按比例大小压缩方法(根据路径获取图片并压缩)
private Bitmap getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空

newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
float hh = 800f;//这里设置高度为800f
float ww = 480f;//这里设置宽度为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);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
}
//图片按比例大小压缩方法(根据Bitmap图片压缩)
private Bitmap comp(Bitmap image) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
if( baos.toByteArray().length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//这里压缩50%,把压缩后的数据存放到baos中
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
float hh = 800f;//这里设置高度为800f
float ww = 480f;//这里设置宽度为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);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
isBm = new ByteArrayInputStream(baos.toByteArray());
bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
}

//质量压缩方法
private Bitmap compressImage(Bitmap image) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while ( baos.toByteArray().length / 1024>100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
options -= 10;//每次都减少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
return bitmap;
}

免责声明:文章转载自《Bitmap 转字节流字符, 压缩等处理》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇软件包管理 之 软件在线升级更新yum 图形工具介绍【273】利用ArcPy建立处理数据的脚本下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

【Android】缩略图Thumbnails

在Android,多媒体文件(视频和图片)都是有缩略图的,在很多应用中,我们需要获取这些缩略图。比如最近在做一个类似相册的应用,需要扫描相册里面的图片,然后获取其缩略图,使用GridView去展示缩略图,当点击之后,我们需要获取其原始图,所以相关的需求如下: 1)获取缩略图(一个问题是:是否所有的图片以及视频都有缩略图?); 2)将缩略图和原始图关联起来;...

C#实现图片的无损压缩

/// <summary> /// 图像缩略图处理 /// </summary> /// <param name="bytes">图像源数据</param> /// <param name="compression">压缩质量 1-100</param> /// <param...

算法---BitMap

问题: 假设有3亿个整数(范围0-2亿),如何判断某一个树是否存在。局限条件一台机器,内存500m。 常规的思路:我们可以将数据存到一个集合中,然后判断某个数是否存在;或者用一个等长的数组来表示,每个数对应的索引位置,存在就标记为1,不存在0。当然如果设备条件允许,上面的这方案是可行的。 但是现在我们内存只有500m。Int类型4个字节。 单单是这3亿个数...

关于View转化成bitmap保存成图片

产品今天说项目分享时要分享出一张  封面图片 + 几行文字 + 二维码图片 的图片。 思索了一下 封面图片和二维码图片让后台给接口得到地址, 主要还是找个方式得到一个包含这些内容的图片。于是就想能不能将View转化成bitmap对象 然后就走了一遍各个前辈的路 整理了下原理和思路。        根据产品的需求  我要实现的步骤  把所有需要的集合在一个V...

iOS图片压缩

项目中常会遇到,上传图片的操作,由于iPhone手机直接拍照的图片往往比较大,一般3-4M,如果直接上传不做处理会浪费用户很多流量,再者有很多场景并不需要高清图片,所以在上传图片前对图片进行压缩,是很有必要的。 1.OC中的UIKit中提供了现成的压缩函数UIImageJPEGRepresentation(UIImage * __nonnull image...

Golang压缩Jpeg图片和PNG图片

博主一直在维护一个导出PDF的服务,但是这个服务导出的PDF文件是真的巨大,动辄就上百MB。这里面主要是图片占据了大多数体积,所以考虑在导出前压缩一下图片。 Jpeg的图片压缩是很好做的,因为jpeg这个协议本身就支持调整图片质量的。在golang中,我们只需要使用标准库的image/jpeg,将图片从二进制数据解码后,降低质量再编码为二进制数据即可实现...