private class BmpThread extends Thread {public BmpThread() {}public void run() {super.run();try {Resources r = getResources();// 以数据流的方式读取资源InputStream is = r.openRawResource(R.drawable.test);BitmapDrawable bmpDraw = new BitmapDrawable(is);Bitmap bmp = bmpDraw.getBitmap();PrintBmp(10, bmp);} catch (Exception e) {e.printStackTrace();} finally {}}}
public void PrintBmp(int startx, Bitmap bitmap) throws IOException {// byte[] start1 = { 0x0d,0x0a};byte[] start2 = { 0x1D, 0x76, 0x30, 0x30, 0x00, 0x00, 0x01, 0x00 };int width = bitmap.getWidth() + startx;int height = bitmap.getHeight();Log.e(TAG,"width: "+width+" height :"+height);if (width > 384)width = 384;int tmp = (width + 7) / 8;byte[] data = new byte[tmp];byte xL = (byte) (tmp % 256);byte xH = (byte) (tmp / 256);start2[4] = xL;start2[5] = xH;start2[6] = (byte) (height % 256);;start2[7] = (byte) (height / 256);;m_Device.sendData(start2);for (int i = 0; i < height; i++) {for (int x = 0; x < tmp; x++)data[x] = 0;for (int x = startx; x < width; x++) {int pixel = bitmap.getPixel(x - startx, i);if (Color.red(pixel) == 0 || Color.green(pixel) == 0|| Color.blue(pixel) == 0) {// 高位在左,所以使用128 右移data[x / 8] += 128 >> (x % 8);// (byte) (128 >> (y % 8));}}while ((printer_status & 0x13) != 0) {Log.e(TAG, "printer_status=" + printer_status);try {Thread.sleep(50);} catch (InterruptedException e) {}}m_Device.sendData(data); }}