Android Bitmap保存为.bmp格式,图像转化为黑白图片
2016-10-28 02:35
1836 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。
[java] view
plain copy
/**
* 把一个View的对象转换成bitmap
*/
static Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
//能画缓存就返回false
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
Log.e("BtPrinter", "failed getViewBitmap(" + v + ")", new RuntimeException());
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
/**
* 将彩色图转换为黑白图
*
* @param 位图
* @return 返回转换好的位图
*/
public static Bitmap convertToBlackWhite(Bitmap bmp) {
int width = bmp.getWidth(); // 获取位图的宽
int height = bmp.getHeight(); // 获取位图的高
int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(newBmp, 380, 460);
return resizeBmp;
}
/**
* 将Bitmap存为 .bmp格式图片
* @param bitmap
*/
private void saveBmp(Bitmap bitmap) {
if (bitmap == null)
return;
// 位图大小
int nBmpWidth = bitmap.getWidth();
int nBmpHeight = bitmap.getHeight();
// 图像数据大小
int bufferSize = nBmpHeight * (nBmpWidth * 3 + nBmpWidth % 4);
try {
// 存储文件名
String filename = "/sdcard/test.bmp";
File file = new File(filename);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fileos = new FileOutputStream(filename);
// bmp文件头
int bfType = 0x4d42;
long bfSize = 14 + 40 + bufferSize;
int bfReserved1 = 0;
int bfReserved2 = 0;
long bfOffBits = 14 + 40;
// 保存bmp文件头
writeWord(fileos, bfType);
writeDword(fileos, bfSize);
writeWord(fileos, bfReserved1);
writeWord(fileos, bfReserved2);
writeDword(fileos, bfOffBits);
// bmp信息头
long biSize = 40L;
long biWidth = nBmpWidth;
long biHeight = nBmpHeight;
int biPlanes = 1;
int biBitCount = 24;
long biCompression = 0L;
long biSizeImage = 0L;
long biXpelsPerMeter = 0L;
long biYPelsPerMeter = 0L;
long biClrUsed = 0L;
long biClrImportant = 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);
// 像素扫描
byte bmpData[] = new byte[bufferSize];
int wWidth = (nBmpWidth * 3 + nBmpWidth % 4);
for (int nCol = 0, nRealCol = nBmpHeight - 1; nCol < nBmpHeight; ++nCol, --nRealCol)
for (int wRow = 0, wByteIdex = 0; wRow < nBmpWidth; wRow++, wByteIdex += 3) {
int clr = 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 (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void writeWord(FileOutputStream stream, int value) throws IOException {
byte[] b = new byte[2];
b[0] = (byte) (value & 0xff);
b[1] = (byte) (value >> 8 & 0xff);
stream.write(b);
}
protected void writeDword(FileOutputStream stream, long value) throws IOException {
byte[] b = new byte[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);
}
protected void writeLong(FileOutputStream stream, long value) throws IOException {
byte[] b = new byte[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);
}
[java] view
plain copy
/**
* 把一个View的对象转换成bitmap
*/
static Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
//能画缓存就返回false
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
Log.e("BtPrinter", "failed getViewBitmap(" + v + ")", new RuntimeException());
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
/**
* 将彩色图转换为黑白图
*
* @param 位图
* @return 返回转换好的位图
*/
public static Bitmap convertToBlackWhite(Bitmap bmp) {
int width = bmp.getWidth(); // 获取位图的宽
int height = bmp.getHeight(); // 获取位图的高
int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(newBmp, 380, 460);
return resizeBmp;
}
/**
* 将Bitmap存为 .bmp格式图片
* @param bitmap
*/
private void saveBmp(Bitmap bitmap) {
if (bitmap == null)
return;
// 位图大小
int nBmpWidth = bitmap.getWidth();
int nBmpHeight = bitmap.getHeight();
// 图像数据大小
int bufferSize = nBmpHeight * (nBmpWidth * 3 + nBmpWidth % 4);
try {
// 存储文件名
String filename = "/sdcard/test.bmp";
File file = new File(filename);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fileos = new FileOutputStream(filename);
// bmp文件头
int bfType = 0x4d42;
long bfSize = 14 + 40 + bufferSize;
int bfReserved1 = 0;
int bfReserved2 = 0;
long bfOffBits = 14 + 40;
// 保存bmp文件头
writeWord(fileos, bfType);
writeDword(fileos, bfSize);
writeWord(fileos, bfReserved1);
writeWord(fileos, bfReserved2);
writeDword(fileos, bfOffBits);
// bmp信息头
long biSize = 40L;
long biWidth = nBmpWidth;
long biHeight = nBmpHeight;
int biPlanes = 1;
int biBitCount = 24;
long biCompression = 0L;
long biSizeImage = 0L;
long biXpelsPerMeter = 0L;
long biYPelsPerMeter = 0L;
long biClrUsed = 0L;
long biClrImportant = 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);
// 像素扫描
byte bmpData[] = new byte[bufferSize];
int wWidth = (nBmpWidth * 3 + nBmpWidth % 4);
for (int nCol = 0, nRealCol = nBmpHeight - 1; nCol < nBmpHeight; ++nCol, --nRealCol)
for (int wRow = 0, wByteIdex = 0; wRow < nBmpWidth; wRow++, wByteIdex += 3) {
int clr = 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 (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void writeWord(FileOutputStream stream, int value) throws IOException {
byte[] b = new byte[2];
b[0] = (byte) (value & 0xff);
b[1] = (byte) (value >> 8 & 0xff);
stream.write(b);
}
protected void writeDword(FileOutputStream stream, long value) throws IOException {
byte[] b = new byte[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);
}
protected void writeLong(FileOutputStream stream, long value) throws IOException {
byte[] b = new byte[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);
}
相关文章推荐
- Android Bitmap保存为.bmp格式,图像转化为黑白图片
- Android Bitmap保存为.bmp格式,图像转化为黑白图片
- Android 玩转图片(读取图片转化Bitmap,保存本地,采样压缩)
- Android中几种图像特效处理的小技巧,比如圆角,倒影,还有就是图片缩放,Drawable转化为Bitmap,Bitmap转化为Drawable等等
- Android ImageUtils 图片工具类,包括:小米从相册调图像空指针,三星旋转照片,压缩图片,保存Bitmap到本地,获取本地视频的缩略图,毛化玻璃效果,代码以后会更新
- android中Bitmap图像处理 修改图片大小以及保存时的文件大小
- android图像绘制(五)画布保存为指定格式/大小的图片
- android中Bitmap图像处理 修改图片大小以及保存时的文件大小
- 分享的是Android图像特效处理的小技巧,比如圆角、倒影、还有就是图片缩放、Drawable转化为Bitmap、Bitmap转化为Drawable等等
- Android图像处理之Bitmap 生成图片 解析 保存文件 获取 问题
- 【转】android:网络图片转为bitmap 保存至SD卡中
- vc6.0使用gdi+在内存中绘图并将其保存为bmp,jpg,gif,png等格式的图片
- GDI+ 将BMP 32图像转换为JPG文件格式保存
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byt
- VB6.0用GDI+保存图片为JPG、TIFF、PNG、GIF、BMP等格式
- PictureBox图像控件保存透明色BMP、JPG图片
- 利用jpeglib压缩各种图像格式为jpg格式,linux下bmp转化为jpeg程序源代码
- Graphics->BitmapDecode 介绍Android 平台对图像的解码功能,Android平台支持PNG, JPEG图像格式,并可以支持 gif动画。
- android图像绘制(五)——画布保存为图片