How to convert /assets/image.png to byte[] ?
I've tried like that (based on solution found on SO):
public void printimage(View view) {
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("logo_print.png");
byte[] bytesLogo = IOUtils.toByteArray(inputStream);
int ret = printer.printImage(bytesLogo);
if (ret < 0) {
Toast(context, "printimage fail");
}
Toast(context, "image printed :)");
}
catch (IOException e){
Log.e("message: ", e.getMessage());
Toast(context, "printimage: convert image to Bytes fail");
}
}
printImage — is declared in package like this:
public class Cprinter {
public native int printImage(byte[] bytes);
}
But application crashes on print printimage(), error is "Native method not found: android.pt.Cprinter.printImage:([B)I"
I've converted byte to string (bytesLogo.toString()), every execution of this command returns different results: "[B@40d7c798", "[B@40d848e0", "[B@40d59ff0", & etc.
Purpose: I have an android device with internal printer of receipts. Vendor has provided library (libfile.so) and sample source for developing own software for device. Printing Simple text is OK, but i have a troubles with printing of an Image (logo).
Here is the vendors` tiny documentation.
p.s:I'm newbie in Java.
Try this
InputStream inputStream = getAssets().open("logo_print.png");
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
byte file[] = output.toByteArray();
the byte array contains
1B 2A n ml mh converted data 1B 4A 00
1B 2A -- begin the block
n - printing mode
a) Q110 support 4 kinds of printing modes,as follow:
n=0x21: 24-point double-density;
n=0x20: 24-point single-density;
n=0x01: 8-point double-density;
n=0x00: 8-point single-density;
b) NXP only support n=0x21: 24-point double-density;
converted data
1B 4A 00 end the block and execute print (print start)
like wise
byte[0] = 0x1B;
byte[1] = 0x2A;
byte[2] = 0x21; // 24-point double-density;
and so on...
Copy your image into drawable folder. Then follow the steps:
Drawable drawable= getResources().getDrawable(R.drawable.image);
Type cast into BitmapDrawable,
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte[] buffer= stream.toByteArray();
You can also try below code for asset folder image:
InputStream stream= null;
try {
stream = getAssets().open("fileName.extension");
byte[] fileBytes=new byte[stream.available()];
stream.read(fileBytes);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
This works for any type of file in the asset folder
InputStream is = getAssets().open("image.png");
byte[] fileBytes=new byte[is.available()];
is.read( fileBytes);
is.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With