how to print picture


 

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);  
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值