Android bitmap转byte
逆转start协议输出
private static byte[] bitmap2byte(Bitmap bmp) throws Exception{
return bitmap2byte(bmp, null);
} private static byte[] bitmap2byte(Bitmap bmp, StarReverseConfig config) throws Exception{
Long s = System.currentTimeMillis();
String TAG = "Start Bitmap2byte";
int imgWidth = bmp.getWidth();
int imgHeight = bmp.getHeight(); //头 无配置
byte[] header = { 0x1b, 0x2a, 0x72, 0x42,
0x1b, 0x2a, 0x72, 0x61,
0x1b, 0x2a, 0x72, 0x41 }; //参数 配置 Set print speed : 1B1E74
//0x1b, 0x2a, 0x72, 0x6d, 0x6c, 0x24, 0x00 1b 2a 72 6d 6c n 00; Set raster left margin: n x 8
byte[] params = { };
//0x1b, 0x6c, 0x24 Set raster left margin
//0x1b, 0x1d, 0x41 n1 n2 Point absolute position
//尾 无配置
byte[] footer = { 0x1b, 0x2a, 0x72, 0x65, 0x31, 0x33, 0x00, 0x1b, 0x0c, 0x19, 0x00 };
//Storellet
//byte[] header = {0x1b, 0x2a, 0x72, 0x42, 0x1b, 0x1d, 0x03, 0x04, 0x00, 0x00, 0x17, 0x00, 0x1b, 0x1e, 0x45, 0x00, 0x1b, 0x06, 0x01, 0x00, 0x1b, 0x06, 0x01, 0x00, 0x1b, 0x1d, 0x03, 0x03, 0x00, 0x00, 0x1b, 0x2a, 0x72, 0x41, 0x1b, 0x2a, 0x72, 0x50, 0x30, 0x00, 0x1b, 0x2a, 0x72, 0x45, 0x31, 0x00, 0x1b, 0x2a, 0x72, 0x59, 0x30, 0x30, 0x33, 0x00 };
//Storellet
//byte[] footer = { 0x1b, 0x2a, 0x72, 0x59, 0x30, 0x30, 0x31, 0x00, 0x1b, 0x2a, 0x72, 0x65, 0x31, 0x33, 0x00, 0x1b, 0x0c, 0x19, 0x1b, 0x2a, 0x72, 0x46, 0x31, 0x33, 0x00, 0x1b, 0x0c, 0x00, 0x1b, 0x2a, 0x72, 0x42, 0x1b, 0x2a, 0x72, 0x42, 0x1b, 0x1d, 0x03, 0x04, 0x00, 0x00, 0x17, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 每行的字节数
int lineBytes = imgWidth / 8 + (imgWidth % 8 > 0 ? 1 : 0); //位移
int movePosition = config != null ? config.getSetLeftWidth() / 8 : 0; Log.i(TAG,"reverse lineBytes:" + lineBytes + ", imgWidth:" + imgWidth + ", imgHeight:" + imgHeight);
// 总字节长度
byte[] srcbytes = new byte[header.length + params.length + footer.length + imgHeight * (lineBytes + 3 + movePosition)]; int srclen = 0;
System.arraycopy(header, 0, srcbytes, srclen, header.length);
srclen = srclen + header.length;
System.arraycopy(params, 0, srcbytes, srclen, params.length);
srclen = srclen + params.length; byte n1byte = (byte)((lineBytes + movePosition) % 256);
byte n2byte = (byte)((lineBytes + movePosition) / 256);
byte tmp = 0;
byte p1 = 0x01, p0 = 0x00;
int xcnt = 3; // x方向上的计数 for (int y = 0; y < imgHeight; y++) {
byte[] lineBytesArray = new byte[lineBytes + 3 + movePosition];
//System.arraycopy(movePosition, 0, lineBytesArray, 0, movePosition.length);
lineBytesArray[0] = (byte)0x62;
lineBytesArray[1] = n1byte;
lineBytesArray[2] = n2byte; xcnt = 3; // x方向上的计数初始值
for (int i = 0; i < movePosition; i++) {
lineBytesArray[xcnt] = 0x00;
xcnt++;
} for (int x = 0; x < imgWidth; x++) {
if (x % 8 == 0 || x == imgWidth -1) {
if(x != 0){
lineBytesArray[xcnt] = tmp;
xcnt++;
}
tmp = 0;
}
int r = bmp.getPixel(x, y);
if (r == Color.BLACK) {
tmp += p1 * Math.pow(2, 7 - x % 8); //高位开始
} else {
tmp += p0 * Math.pow(2, 7 - x % 8);
}
}
System.arraycopy(lineBytesArray, 0, srcbytes, srclen, lineBytesArray.length);
srclen = srclen + lineBytesArray.length;
Thread.yield();
} System.arraycopy(footer, 0, srcbytes, srclen, footer.length);
Log.i(TAG,"reverse receipt is :" + srcbytes.length + ", all Time:" + (System.currentTimeMillis() - s));
return srcbytes;
}
Android bitmap转byte的更多相关文章
- Android Bitmap Drawable byte[] InputStream 相互转换方法
用android处理图片的时候,由于得到的资源不一样,所以经常要在各种格式中相互转化,以下介绍了 Bitmap Drawable byte[] InputStream 之间的转换方法: import ...
- Android中Bitmap, Drawable, Byte,ID之间的转化
Android中Bitmap, Drawable, Byte,ID之间的转化 1. Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...
- Android Drawable、Bitmap、byte[]之间的转换
转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitma ...
- android开发之Bitmap 、byte[] 、 Drawable之间的相互转换
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- [翻译]开发文档:android Bitmap的高效使用
内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...
- Android Bitmap 和 ByteArray的互相转换
Android Bitmap 和 ByteArray的互相转换 移动平台图像处理,需要将图像传给native处理,如何传递?将bitmap转换成一个 byte[] 方便传递也方便cpp代码直接处理图像 ...
- Android-Drawable、Bitmap、byte[]、资源文件相互转换
我们在Android的开发中,经常可以遇到图片的处理,当中,有很多是 Bitmap.Drawable.byte[]和资源文件它们直接相互转换. 今天就此总结一下: 1.资源文件转为Drawable 2 ...
- android Bitmap类方法属性 详细说明
(转:http://blog.csdn.net/ymangu666/article/details/37729109) 1. BitMap类public void recycle()——回收位图占用 ...
随机推荐
- 64位Ubuntu系统安装OpenCV 2.4.x+ffmpeg 完美解决方案
http://www.bfcat.com/index.php/2012/09/64bits-ubuntu-opencv-2-4-x-ffmpeg/
- rxjava 调用retrofit执行网络请求的过程
retrofit流程图 -1.RxJava调用Retrofit,从requestGtPushSaeUserInfo()中获得被观察者observable,然后new一个观察者向它订阅 0.从业务中 ...
- Missing artifact com.sun:tools:jar:1.7解决方案
在配置Java + Robotframework时遇到的问题“Missing artifact com.sun:tools:jar” 1. 先检查一下eclipse或STS中的JDK路径配置是否正确( ...
- Ubuntu上交叉编译valgrind for Android 4.0.4的过程与注意事项
编译环境:Ubuntu x86_64(Linux root 2.6.32-45-generic #101-Ubuntu SMP Mon Dec 3 15:39:38 UTC 2012 x86_64 G ...
- 深入php redis pconnect
深入php redis pconnect pconnect是phpredis中用于client连接server的api. API文档中的一句原文: The connection will not be ...
- Redis学习笔记-事务控制篇(Centos7)
一.事务控制 1.简单事务控制 redis可以使用mult命令将之后的命令都存放在队列中,只有使用exec命令时才全部执行. 127.0.0.1:6379> multi OK 127.0.0.1 ...
- StreamSets Data Collector Edge 说明
Data Collector Edge 是不包含界面的agent 安装 下载包 https://streamsets.com/opensource tar xf streamsets-datacoll ...
- nomad 集群搭建
比较简单的集群搭建 一个server 三个client (单机) 参考代码 https://github.com/rongfengliang/nomad-cluster-demo server 配置 ...
- IT项目管理工具总结
IT项目管理工具总结 俗话说"工欲善其事必先利其器",在一个项目开发流程中,如果搭配一个比较完善的项目管理工具,必将取得事半功倍的效果.本文搜集了目前项目管理界比较有规模的管理工具 ...
- 【monkeyrunner】monkeyrunner脚本录制和回放
脚本录制 1.连接你已经打开调试模式的ANDROID设备或模拟器,输入adb devices 2.运行录制脚本.在cmd窗口输入 monkeyrunner recorder.py #recorder. ...