欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Bitmap详细介绍

程序员文章站 2023-12-05 17:12:34
复制代码 代码如下: package com.testbitmapscale; import java.io.file; import java.io.fileinputs...
复制代码 代码如下:

package com.testbitmapscale;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.util.iterator;
import com.testbitmapscale.r.drawable;
import android.app.activity;
import android.graphics.bitmap;
import android.graphics.bitmap.config;
import android.graphics.bitmapfactory;
import android.graphics.canvas;
import android.graphics.matrix;
import android.graphics.paint;
import android.graphics.porterduff.mode;
import android.graphics.porterduffxfermode;
import android.graphics.rect;
import android.graphics.rectf;
import android.graphics.drawable.bitmapdrawable;
import android.graphics.drawable.drawable;
import android.media.thumbnailutils;
import android.os.bundle;
import android.os.environment;
import android.view.view;
import android.widget.imageview;
//方法:
//1 生成圆角bitmap图片
//2 生成bitmap缩量图
//3 压缩图片场长宽以及kb
//注意:
//以上代码,测试其中一个方法时最好注释掉其余的代码
public class mainactivity extends activity {
private imageview imageview;
private bitmap copyrawbitmap1;
private bitmap copyrawbitmap2;
private bitmap copyrawbitmap3;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
imageview = (imageview) findviewbyid(r.id.imageview);
//第一种方式:从资源文件中得到图片
bitmap rawbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.haha);
copyrawbitmap1=rawbitmap;
copyrawbitmap2=rawbitmap;
copyrawbitmap3=rawbitmap;
//第二种方式:从sd卡中得到图片(方法1)
string sdcarepath=environment.getexternalstoragedirectory().tostring();
string filepath=sdcarepath+"/"+"haha.jpg";
bitmap rawbitmap1 = bitmapfactory.decodefile(filepath, null);
//第二种方式:从sd卡中得到图片(方法2)
inputstream inputstream=getbitmapinputstreamfromsdcard("haha.jpg");
bitmap rawbitmap2 = bitmapfactory.decodestream(inputstream);

//————>以下为将设置图片的圆角
bitmap roundcornerbitmap=this.toroundcorner(rawbitmap, 40);
imageview.setimagebitmap(roundcornerbitmap);
//————>以上为将设置图片的圆角

//————>以下为将图片高宽和的大小kb压缩
// 得到图片原始的高宽
int rawheight = rawbitmap.getheight();
int rawwidth = rawbitmap.getwidth();
// 设定图片新的高宽
int newheight = 500;
int newwidth = 500;
// 计算缩放因子
float heightscale = ((float) newheight) / rawheight;
float widthscale = ((float) newwidth) / rawwidth;
// 新建立矩阵
matrix matrix = new matrix();
matrix.postscale(heightscale, widthscale);
// 设置图片的旋转角度
//matrix.postrotate(-30);
// 设置图片的倾斜
//matrix.postskew(0.1f, 0.1f);
//将图片大小压缩
//压缩后图片的宽和高以及kb大小均会变化
bitmap newbitmap = bitmap.createbitmap(rawbitmap, 0, 0, rawwidth,rawwidth, matrix, true);
// 将bitmap转换为drawable
drawable newbitmapdrawable = new bitmapdrawable(newbitmap);
imageview.setimagedrawable(newbitmapdrawable);
//然后将bitmap保存到sdcard中,方便于原图片的比较
this.compressandsavebitmaptosdcard(newbitmap, "xx100.jpg", 80);
//问题:
//原图大小为625x690 90.2kb
//如果设置图片500x500 压缩后大小为171kb.即压缩后kb反而变大了.
//原因是将:compress(bitmap.compressformat.jpeg, quality, fileoutputstream);
//第二个参数quality设置得有些大了(比如100).
//常用的是80,刚设100太大了造成的.
//————>以上为将图片高宽和的大小kb压缩


//————>以下为将图片的kb压缩,宽高不变
this.compressandsavebitmaptosdcard(copyrawbitmap1,"0011fa.jpg",80);
//————>以上为将图片的kb压缩,宽高不变

//————>以下为获取sd卡图片的缩略图方法1
string sdcarepath1=environment.getexternalstoragedirectory().tostring();
string filepath1=sdcarepath1+"/"+"haha.jpg";
bitmap bitmapthumbnail1=this.getbitmapthumbnail(filepath1);
imageview.setimagebitmap(bitmapthumbnail1);
//————>以上为获取sd卡图片的缩略图方法1

//————>以下为获取sd卡图片的缩略图方法2
string sdcarepath2=environment.getexternalstoragedirectory().tostring();
string filepath2=sdcarepath2+"/"+"haha.jpg";
bitmap tempbitmap=bitmapfactory.decodefile(filepath2);
bitmap bitmapthumbnail2=thumbnailutils.extractthumbnail(tempbitmap, 100, 100);
imageview.setimagebitmap(bitmapthumbnail2);
//————>以上为获取sd卡图片的缩略图方法2

}
//读取sd卡下的图片
private inputstream getbitmapinputstreamfromsdcard(string filename){
if (environment.getexternalstoragestate().equals(environment.media_mounted)) {
string sdcarepath=environment.getexternalstoragedirectory().tostring();
string filepath=sdcarepath+file.separator+filename;
file file=new file(filepath);
try {
fileinputstream fileinputstream=new fileinputstream(file);
return fileinputstream;
} catch (exception e) {
e.printstacktrace();
}

}
return null;
}


//获取sdcard的目录路径功能
private string getsdcardpath() {
string sdcardpath = null;
// 判断sdcard是否存在
boolean issdcardexist = environment.getexternalstoragestate().equals(android.os.environment.media_mounted);
if (issdcardexist) {
sdcardpath = environment.getexternalstoragedirectory().tostring();
}
return sdcardpath;
}
//压缩且保存图片到sdcard
private void compressandsavebitmaptosdcard(bitmap rawbitmap,string filename,int quality){
string savefilepaht=this.getsdcardpath()+file.separator+filename;
file savefile=new file(savefilepaht);
if (!savefile.exists()) {
try {
savefile.createnewfile();
fileoutputstream fileoutputstream=new fileoutputstream(savefile);
if (fileoutputstream!=null) {
//imagebitmap.compress(format, quality, stream);
//把位图的压缩信息写入到一个指定的输出流中
//第一个参数format为压缩的格式
//第二个参数quality为图像压缩比的值,0-100.0 意味着小尺寸压缩,100意味着高质量压缩
//第三个参数stream为输出流
rawbitmap.compress(bitmap.compressformat.jpeg, quality, fileoutputstream);
}
fileoutputstream.flush();
fileoutputstream.close();
} catch (ioexception e) {
e.printstacktrace();

}
}
}

//获取图片的缩略图
private bitmap getbitmapthumbnail(string filepath){
bitmapfactory.options options=new bitmapfactory.options();
//true那么将不返回实际的bitmap对象,不给其分配内存空间但是可以得到一些解码边界信息即图片大小等信息
options.injustdecodebounds=true;
//此时rawbitmap为null
bitmap rawbitmap = bitmapfactory.decodefile(filepath, options);
if (rawbitmap==null) {
system.out.println("此时rawbitmap为null");
}
//insamplesize表示缩略图大小为原始图片大小的几分之一,若该值为3
//则取出的缩略图的宽和高都是原始图片的1/3,图片大小就为原始大小的1/9
//计算samplesize
int samplesize=computesamplesize(options, 150, 200*200);
//为了读到图片,必须把options.injustdecodebounds设回false
options.injustdecodebounds = false;
options.insamplesize = samplesize;
//原图大小为625x690 90.2kb
//测试调用computesamplesize(options, 100, 200*100);
//得到samplesize=8
//得到宽和高位79和87
//79*8=632 87*8=696
bitmap thumbnailbitmap=bitmapfactory.decodefile(filepath, options);
//保存到sd卡方便比较
this.compressandsavebitmaptosdcard(thumbnailbitmap, "15.jpg", 80);
return thumbnailbitmap;
}

//参考资料:
//http://my.csdn.net/zljk000/code/detail/18212
//第一个参数:原本bitmap的options
//第二个参数:希望生成的缩略图的宽高中的较小的值
//第三个参数:希望生成的缩量图的总像素
public static int computesamplesize(bitmapfactory.options options,int minsidelength, int maxnumofpixels) {
int initialsize = computeinitialsamplesize(options, minsidelength,maxnumofpixels);
int roundedsize;
if (initialsize <= 8) {
roundedsize = 1;
while (roundedsize < initialsize) {
roundedsize <<= 1;
}
} else {
roundedsize = (initialsize + 7) / 8 * 8;
}
return roundedsize;
}

private static int computeinitialsamplesize(bitmapfactory.options options,int minsidelength, int maxnumofpixels) {
//原始图片的宽
double w = options.outwidth;
//原始图片的高
double h = options.outheight;
system.out.println("========== w="+w+",h="+h);
int lowerbound = (maxnumofpixels == -1) ? 1 : (int) math.ceil(math
.sqrt(w * h / maxnumofpixels));
int upperbound = (minsidelength == -1) ? 128 : (int) math.min(
math.floor(w / minsidelength), math.floor(h / minsidelength));
if (upperbound < lowerbound) {
// return the larger one when there is no overlapping zone.
return lowerbound;
}
if ((maxnumofpixels == -1) && (minsidelength == -1)) {
return 1;
} else if (minsidelength == -1) {
return lowerbound;
} else {
return upperbound;
}
}

/**
* @param bitmap 需要修改的图片
* @param pixels 圆角的弧度
* @return 圆角图片
*/
//参考资料:
//http://blog.csdn.net/c8822882/article/details/6906768
public bitmap toroundcorner(bitmap bitmap, int pixels) {
bitmap roundcornerbitmap = bitmap.createbitmap(bitmap.getwidth(), bitmap.getheight(), config.argb_8888);
canvas canvas = new canvas(roundcornerbitmap);
int color = 0xff424242;//int color = 0xff424242;
paint paint = new paint();
paint.setcolor(color);
//防止锯齿
paint.setantialias(true);
rect rect = new rect(0, 0, bitmap.getwidth(), bitmap.getheight());
rectf rectf = new rectf(rect);
float roundpx = pixels;
//相当于清屏
canvas.drawargb(0, 0, 0, 0);
//先画了一个带圆角的矩形
canvas.drawroundrect(rectf, roundpx, roundpx, paint);
paint.setxfermode(new porterduffxfermode(mode.src_in));
//再把原来的bitmap画到现在的bitmap!!!注意这个理解
canvas.drawbitmap(bitmap, rect, rect, paint);
return roundcornerbitmap;
}

}