我使用热敏打印机打印图像,并且效果很好。但是,当我尝试将字符串转换为位图然后打印时,会得到奇怪的打印结果。
这是我将字符串转换为位图的方式
public Bitmap textAsBitmap(String text, float textSize, int textColor) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setTextAlign(Paint.Align.LEFT);
float baseline = -paint.ascent(); // ascent() is negative
int width = (int) (paint.measureText(text) + 0.5f); // round
int height = (int) (baseline + paint.descent() + 0.5f);
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawText(text, 0, baseline, paint);
return image;
}
这是我的打印方法
public void printPhoto2(Bitmap bmp) {
try {
if(bmp!=null){
byte[] command = Utils.decodeBitmap(bmp);
mmOutputStream.write(PrinterCommands.ESC_ALIGN_CENTER);
printText2(command);
}else{
Log.e("Print Photo error", "the file isn't exists");
}
} catch (Exception e) {
e.printStackTrace();
Log.e("PrintTools", "the file isn't exists");
}
}
private void printText2(byte[] msg) {
try {
// Print normal text
mmOutputStream.write(msg);
printNewLine();
} catch (IOException e) {
e.printStackTrace();
}
}
我这样打印
printPhoto2(textAsBitmap(string,30f,R.color.BlackColorOut));
当打印诸如“ Hello world”之类的小文字时,这就是我从打印机获得的信息
这是什么问题?