/**
*把一个View的对象转换成bitmap
*/
staticBitmapgetViewBitmap(Viewv){
v.clearFocus();
v.setPressed(false);
//能画缓存就返回false
booleanwillNotCache=v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
intcolor=v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if(color!=0){
v.destroyDrawingCache();
}
v.buildDrawingCache();
BitmapcacheBitmap=v.getDrawingCache();
if(cacheBitmap==null){
Log.e("BtPrinter","failedgetViewBitmap("+v+")",newRuntimeException());
returnnull;
}
Bitmapbitmap=Bitmap.createBitmap(cacheBitmap);
//Restoretheview
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
returnbitmap;
}
/**
*将彩色图转换为黑白图
*
*@param位图
*@return返回转换好的位图
*/
publicstaticBitmapconvertToBlackWhite(Bitmapbmp){
intwidth=bmp.getWidth();//获取位图的宽
intheight=bmp.getHeight();//获取位图的高
int[]pixels=newint[width*height];//通过位图的大小创建像素点数组
bmp.getPixels(pixels,0,width,0,0,width,height);
intalpha=0xFF<
for(inti=0;i
for(intj=0;j
intgrey=pixels[width*i+j];
intred=((grey&0x00FF0000)>>16);
intgreen=((grey&0x0000FF00)>>8);
intblue=(grey&0x000000FF);
grey=(int)(red*0.3+green*0.59+blue*0.11);
grey=alpha|(grey<
pixels[width*i+j]=grey;
}
}
BitmapnewBmp=Bitmap.createBitmap(width,height,Config.RGB_565);
newBmp.setPixels(pixels,0,width,0,0,width,height);
BitmapresizeBmp=ThumbnailUtils.extractThumbnail(newBmp,380,460);
returnresizeBmp;
}
/**
*将Bitmap存为.bmp格式图片
*@parambitmap
*/
privatevoidsaveBmp(Bitmapbitmap){
if(bitmap==null)
return;
//位图大小
intnBmpWidth=bitmap.getWidth();
intnBmpHeight=bitmap.getHeight();
//图像数据大小
intbufferSize=nBmpHeight*(nBmpWidth*3+nBmpWidth%4);
try{
//存储文件名
Stringfilename="/sdcard/test.bmp";
Filefile=newFile(filename);
if(!file.exists()){
file.createNewFile();
}
FileOutputStreamfileos=newFileOutputStream(filename);
//bmp文件头
intbfType=0x4d42;
longbfSize=14+40+bufferSize;
intbfReserved1=0;
intbfReserved2=0;
longbfOffBits=14+40;
//保存bmp文件头
writeWord(fileos,bfType);
writeDword(fileos,bfSize);
writeWord(fileos,bfReserved1);
writeWord(fileos,bfReserved2);
writeDword(fileos,bfOffBits);
//bmp信息头
longbiSize=40L;
longbiWidth=nBmpWidth;
longbiHeight=nBmpHeight;
intbiPlanes=1;
intbiBitCount=24;
longbiCompression=0L;
longbiSizeImage=0L;
longbiXpelsPerMeter=0L;
longbiYPelsPerMeter=0L;
longbiClrUsed=0L;
longbiClrImportant=0L;
//保存bmp信息头
writeDword(fileos,biSize);
writeLong(fileos,biWidth);
writeLong(fileos,biHeight);
writeWord(fileos,biPlanes);
writeWord(fileos,biBitCount);
writeDword(fileos,biCompression);
writeDword(fileos,biSizeImage);
writeLong(fileos,biXpelsPerMeter);
writeLong(fileos,biYPelsPerMeter);
writeDword(fileos,biClrUsed);
writeDword(fileos,biClrImportant);
//像素扫描
bytebmpData[]=newbyte[bufferSize];
intwWidth=(nBmpWidth*3+nBmpWidth%4);
for(intnCol=0,nRealCol=nBmpHeight-1;nCol
for(intwRow=0,wByteIdex=0;wRow
intclr=bitmap.getPixel(wRow,nCol);
bmpData[nRealCol*wWidth+wByteIdex]=(byte)Color.blue(clr);
bmpData[nRealCol*wWidth+wByteIdex+1]=(byte)Color.green(clr);
bmpData[nRealCol*wWidth+wByteIdex+2]=(byte)Color.red(clr);
}
fileos.write(bmpData);
fileos.flush();
fileos.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
protectedvoidwriteWord(FileOutputStreamstream,intvalue)throwsIOException{
byte[]b=newbyte[2];
b[0]=(byte)(value&0xff);
b[1]=(byte)(value>>8&0xff);
stream.write(b);
}
protectedvoidwriteDword(FileOutputStreamstream,longvalue)throwsIOException{
byte[]b=newbyte[4];
b[0]=(byte)(value&0xff);
b[1]=(byte)(value>>8&0xff);
b[2]=(byte)(value>>16&0xff);
b[3]=(byte)(value>>24&0xff);
stream.write(b);
}
protectedvoidwriteLong(FileOutputStreamstream,longvalue)throwsIOException{
byte[]b=newbyte[4];
b[0]=(byte)(value&0xff);
b[1]=(byte)(value>>8&0xff);
b[2]=(byte)(value>>16&0xff);
b[3]=(byte)(value>>24&0xff);
stream.write(b);
}