android使用esc/pos指令集打印条形码的demo
用打印图片的方法试过不行,最好有demo,看看是我编码的问题还是别的问题
public static byte[] BitmapToByte(Bitmap b) {
int ww = b.getWidth();
int h = b.getHeight();
int w = (ww - 1) / 8 + 1;
byte[] data = new byte[h * w + 8];
data[0] = 0x1D;
data[1] = 0x76;
data[2] = 0x30;
data[3] = 0x00;
data[4] = (byte) w;// xL
data[5] = (byte) (w >> 8);// xH
data[6] = (byte) h;
data[7] = (byte) (h >> 8);
getAllPixels_gh(b, data);
return data;
}
protected static void getAllPixels_gh(Bitmap bit, byte[] gh) {
int k = bit.getWidth() * bit.getHeight();
int[] pixels = new int[k];
bit.getPixels(pixels, 0, bit.getWidth(), 0, 0, bit.getWidth(), bit.getHeight());
int j = 7;
int index = 8;
for (int i = 0; i < pixels.length; i++) {
int clr = pixels[i];
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
if (j == -1) {
j = 7;
index++;
}
gh[index] = (byte) (gh[index] | (RGB2Gray(red, green, blue) << j));
j--;
}
}
private static byte RGB2Gray(int r, int g, int b) {
return (int) (0.29900 * r + 0.58700 * g + 0.11400 * b)< 150 ? (byte) 1 : (byte) 0;
}