Mukund Pandya Mukund Pandya - 2 months ago 29
Android Question

Print Image by bluetooth printer?

How to print image by thermal bluetooth printer in Android?

I am trying to print image by thermal bluetooth printer.
But , when i am trying to insert code of image in my main java file the output come as a byte code.

So, How can i convert Byte Code into image ?
or how can i give system path for image printing?

Here I am Sharing my code snippet:

private void ImagePrint() {

byte[] rasterImage = {0x1D, 0x76, 0x30, 0x30, 0x20, 0x00, 0x00, 0x01};

Toast.makeText(Collectionplusclick.this, "image printing", Toast.LENGTH_SHORT).show();
File file = new File("Phone storage/image/printimglogo.png");
int size = (int) file.length();
bitmapdata = new byte[size];
try
{
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bitmapdata, 0, bitmapdata.length);
buf.close();
} catch (FileNotFoundException e)
{
} catch (IOException e)
{
}

byte[] totalfooter = new byte[ rasterImage.length+ bitmapdata.length ];

int offset = 0;
System.arraycopy(rasterImage, 0, rasterImage, offset, rasterImage.length);
offset += rasterImage.length;

System.arraycopy(bitmapdata, 0, bitmapdata, offset, bitmapdata.length);
offset += bitmapdata.length;

byte[] senddata = PocketPos.FramePack(PocketPos.FRAME_TOF_PRINT, totalfooter, 0, totalfooter.length);

sendData(senddata);
}

Answer
private byte[] printbyte = {(byte)0x1B,(byte)0x2A,(byte)0x6F,(byte)0x63} 

File file = new File(Environment.getExternalStorageDirectory + 

File.seprator()+"file_name");
FileInputStream fin = new FileInputStream(file);

byte imageContent[] = new byte[(int)file.length()];
fin.read(imageContent);

byte [] width  = hexToBuffer(Integer.toHexString(your_width));
byte [] height = hexToBuffer(Integer.toHexString(your_height));

byte[] imageToPrint = new byte[printbyte.lenght()+imageContent.lenght()+width.lenght()+height.lenght()];

  System.arraycopy(imagetoprint,0,printbyte,0,printbyte.lenght());
  System.arraycopy(imagetoprint,printbyte.lenght(),width ,0,width.lenght());
  System.arraycopy(imagetoprint,width.lenght(),height,0,height.lenght());  
  System.arraycopy(imagetoprint,height.lenght(),imageContent,0,imageContent.lenght()); 

  mmOutputStream.write(imagetoprint); 

Hex to Buffer method -

public static byte[] hexToBuffer(String hexString)
throws NumberFormatException {
int length = hexString.length();
byte[] buffer = new byte[(length + 1) / 2];
boolean evenByte = true;
byte nextByte = 0;
int bufferOffset = 0;

if ((length % 2) == 1) {
    evenByte = false;
}

for (int i = 0; i < length; i++) {
    char c = hexString.charAt(i);
    int nibble; // A "nibble" is 4 bits: a decimal 0..15

    if ((c >= '0') && (c <= '9')) {
        nibble = c - '0';
    } else if ((c >= 'A') && (c <= 'F')) {
        nibble = c - 'A' + 0x0A;
    } else if ((c >= 'a') && (c <= 'f')) {
        nibble = c - 'a' + 0x0A;
    } else {
        throw new NumberFormatException("Invalid hex digit '" + c +
            "'.");
    }

    if (evenByte) {
        nextByte = (byte) (nibble << 4);
    } else {
        nextByte += (byte) nibble;
        buffer[bufferOffset++] = nextByte;
    }

    evenByte = !evenByte;
}

return buffer;
}
Comments