dee*_*lay 2 java android shared-libraries
如何将/assets/image.png转换为byte []?
我已经尝试过了(基于在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");
}
}
Run Code Online (Sandbox Code Playgroud)
printImage —在如下包中声明:
public class Cprinter {
public native int printImage(byte[] bytes);
}
Run Code Online (Sandbox Code Playgroud)
但是应用程序在print printimage()上崩溃,错误是“找不到本机方法:android.pt.Cprinter.printImage:([B] I””
我已将字节转换为字符串(bytesLogo.toString()),此命令的每次执行都会返回不同的结果:“ [B @ 40d7c798”,“ [B @ 40d848e0”,“ [B @ 40d59ff0””等)。
目的:我有一个带有收据内部打印机的android设备。供应商提供了库(libfile.so)和示例源,用于为设备开发自己的软件。打印简单文本是可以的,但是我在打印图像(徽标)时遇到了麻烦。
ps:我是Java的新手。
尝试这个
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();
Run Code Online (Sandbox Code Playgroud)
字节数组包含
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)
Run Code Online (Sandbox Code Playgroud)
像明智的
byte[0] = 0x1B;
byte[1] = 0x2A;
byte[2] = 0x21; // 24-point double-density;
and so on...
Run Code Online (Sandbox Code Playgroud)