android 将bitmap存为 bmp格式图片大小,把bitmap保存成 BMP的格式 并且位深度为1
生成图片的要求:图像格式采用单色位图文件格式(BMP) 要求bmp的位深度为1
参考:
代码有点小瑕疵 : bitmap的图片宽度要求是:8的整数倍
/**
* 将Bitmap存为 .bmp格式图片
*
* @param bitmap
*/
public void saveBmp(Bitmap bitmap) {
if (bitmap == null)
return;
// 位图大小
int nBmpWidth = bitmap.getWidth();
int nBmpHeight = bitmap.getHeight();
// 图像数据大小
int bufferSize = nBmpHeight * nBmpWidth /8 + 4*2;
try {
// 存储文件名
String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "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 = 1;
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 buf[] = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff,0, 0, 0, 0, 0};
fileos.write(buf);
// 像素扫描
int bw = nBmpWidth/8;
byte recv[] = new byte[bw*nBmpHeight];
int[] pixels = new int[nBmpWidth * nBmpHeight];
bitmap.getPixels(pixels, 0, nBmpWidth, 0, 0, nBmpWidth, nBmpHeight);
for(int i = nBmpHeight - 1, ii = 0; i >= 0; i--, ii++){
for(int j = 0; j < nBmpWidth; j++){
int clr = pixels[nBmpWidth * i + j];
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
byte gray = (RGB2Gray(red, green, blue));
recv[(nBmpWidth * ii + j) / 8] = (byte) (recv[(nBmpWidth * ii + j) / 8] | (gray << (7 - ((nBmpWidth * ii + j) % 8))));
}
}
fileos.write(recv);
fileos.flush();
fileos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private byte RGB2Gray(int r, int g, int b) {
return (false ? ((int) (0.29900 * r + 0.58700 * g + 0.11400 * b) > 200)
: ((int) (0.29900 * r + 0.58700 * g + 0.11400 * b) < 200)) ? (byte) 1 : (byte) 0;
}
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格式图片大小,把bitmap保存成 BMP的格式 并且位深度为1相关推荐
- 读取json格式的图片、文字并保存成图片
读取json格式的图片.文字并保存成图片 说明 从数据库中获取到图片.中文.英文信息后,将其合成json文件,图片格式为base64格式,需要将其转成二进制形式,然后将其保存成图片,将中文.英文信息保 ...
- php将视频流逐帧转图片,ffmpeg sdk解码视频帧后保存成BMP或JPG的方法
ffmpeg解码视频的例子可以看官方自带的encode_decode.c. 官方解码保存成ppm,这里接下来保存成BMP或JPG. 原理: 保存BMP是解码成功后,从YUV420转成RGB24,然后构 ...
- js大屏导出图片_js将canvas保存成图片并下载
保存 var arr = [ {locations:[[0,0],[200,200],[0,400]],color:"red"}, {locations:[[0,0],[400,0 ...
- 【转】DCMTK开源库的学习笔记1:将DCM文件保存成BMP文件或数据流(即数组)
转自:https://blog.csdn.net/zssureqh/article/details/8784980 DCMTK开源库介绍: DCMTK是目前最全面实现DICOM3.0标准的开源库,通过 ...
- android 将bitmap存为 bmp格式图片大小,Android Bitmap保存為.bmp格式,圖像轉化為黑白圖片...
/** * 把一個View的對象轉換成bitmap */ static Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(fals ...
- bmp文件格式_一次性解决CAD转换成BMP格式图片的问题
关于BMP格式图片,可能很多小伙伴都遇到得比较多,但在这里还是稍微解释一下吧. 它是Windows操作系统中的标准图像文件格式,大家都知道BMP格式的图片都非常大,那是因为它没有经过任何压缩. 当然, ...
- html虚拟打印转为pdf,Doro PDF Writer 虚拟打印机 – 可将任何格式图片/文档/文件转换成 PDF格式...
在办公领域经常要用到 PDF 格式文档,因此常常有图片.表格.Word 等各种不同格式的文档转换成 PDF 格式的需求.虽然网上有很多在线转换网站或 PDF 转换软件,不过今天推荐的是通用性更强的软件 ...
- html保存当前页面为图片,将html页面保存成图片,图片写入pdf的实现方法(推荐)
需求是一个导出pdf的功能,多方奔走终于实现了,走了不少弯路,而且怀疑现在这个方法仍是弯的. 有个jsPDF 插件可以在前端直接生成pdf,很简便,但不支持IE. 前端: 首先引入 html2can ...
- java 千分位格式话_Java 字符串小数转成千分位格式
java中需要将字符串小数转成千分位格式显示,如下代码,原理是使用正则表达式对整数位进行千分位格式化,然后小数位拼接起来.为什么要这么麻烦的处理,是因为在android程序中使用的NumberForm ...
最新文章
- Java实现MD5(32/16位大小写)加密
- IEEE的论文需要注意的一些
- 014 怪物过滤的设计和实现
- easyuI企业管理系统-实战三 添加功能
- 统计学:回归分析(2)
- VTK:可视化之ScalarVisibility
- struct ethhdr结构体详解
- 无盘服务器磁盘缓存,云更新无盘服务器磁盘缓存器
- 【人工智能中“预测”的知识点】
- linux常用命令(ubuntu)
- C语言标准库<limits.h>
- mongodb用户管理和服务安装
- python熊猫烧香_熊猫烧香完整源代码
- excel小写转大写公式_excel数字小写转大写公式的教程
- 大数据处理的基本流程
- 微信群打卡小程序_签到打卡小程序,哪个更适合你?
- 三菱Q系列总线型项目程序全套,三菱PLC程序
- win10无线信号强度测试软件,一款改善Win10 WiFi 信号的小工具
- 想知道“照片识别文字”的技巧吗?快看这几个方法
- ie打开本地html页面慢,win7使用ie浏览器访问网页显示缓慢、卡死的解决方法