是否有人知道如何將圖像(如* .bmp文件)轉換爲Java中的HEX字符串,以便HEX字符串可以在^ DG命令(zpl中的一個命令)中使用。如何使用^ DG命令將圖像發送到java中的斑馬打印機?
2
A
回答
0
這並不容易。您必須將圖像轉換爲每像素1位b/w,然後計算大小(包括每行的字節數),然後正確組成~DG
命令的參數以存儲它。
或者...
斑馬提供API將圖像轉換爲的GRF(斑馬內部圖像類型)。有一個包中的一羣開發者演示,包括如何打印/圖片作爲GRF存儲在打印機上
希望這有助於演示
3
我寫了一個Java示例那可以做到這一點。 您還可以創建ascii十六進制代碼或壓縮的ZPL代碼並選擇黑度百分比以提高準確性。
相應地更改主方法中的參數。爲了更廣泛的解釋here
*爲不打印此enter
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
public class ZPLConveter {
private int blackLimit = 380;
private int total;
private int widthBytes;
private boolean compressHex = false;
private static Map<Integer, String> mapCode = new HashMap<Integer, String>();
{
mapCode.put(1, "G");
mapCode.put(2, "H");
mapCode.put(3, "I");
mapCode.put(4, "J");
mapCode.put(5, "K");
mapCode.put(6, "L");
mapCode.put(7, "M");
mapCode.put(8, "N");
mapCode.put(9, "O");
mapCode.put(10, "P");
mapCode.put(11, "Q");
mapCode.put(12, "R");
mapCode.put(13, "S");
mapCode.put(14, "T");
mapCode.put(15, "U");
mapCode.put(16, "V");
mapCode.put(17, "W");
mapCode.put(18, "X");
mapCode.put(19, "Y");
mapCode.put(20, "g");
mapCode.put(40, "h");
mapCode.put(60, "i");
mapCode.put(80, "j");
mapCode.put(100, "k");
mapCode.put(120, "l");
mapCode.put(140, "m");
mapCode.put(160, "n");
mapCode.put(180, "o");
mapCode.put(200, "p");
mapCode.put(220, "q");
mapCode.put(240, "r");
mapCode.put(260, "s");
mapCode.put(280, "t");
mapCode.put(300, "u");
mapCode.put(320, "v");
mapCode.put(340, "w");
mapCode.put(360, "x");
mapCode.put(380, "y");
mapCode.put(400, "z");
}
public String convertfromImg(BufferedImage image) throws IOException {
String body = createBody(image);
if(compressHex)
body = encodeHexAscii(body);
return headDoc() + body + footDoc();
}
private String createBody(BufferedImage orginalImage) throws IOException {
StringBuffer sb = new StringBuffer();
Graphics2D graphics = orginalImage.createGraphics();
graphics.drawImage(orginalImage, 0, 0, null);
int height = orginalImage.getHeight();
int width = orginalImage.getWidth();
int rgb, red, green, blue, index=0;
char auxBinaryChar[] = {'0', '0', '0', '0', '0', '0', '0', '0'};
widthBytes = width/8;
if(width%8>0){
widthBytes= (((int)(width/8))+1);
} else {
widthBytes= width/8;
}
this.total = widthBytes*height;
for (int h = 0; h<height; h++)
{
for (int w = 0; w<width; w++)
{
rgb = orginalImage.getRGB(w, h);
red = (rgb >> 16) & 0x000000FF;
green = (rgb >> 8) & 0x000000FF;
blue = (rgb) & 0x000000FF;
char auxChar = '1';
int totalColor = red + green + blue;
if(totalColor>blackLimit){
auxChar = '0';
}
auxBinaryChar[index] = auxChar;
index++;
if(index==8 || w==(width-1)){
sb.append(fourByteBinary(new String(auxBinaryChar)));
auxBinaryChar = new char[]{'0', '0', '0', '0', '0', '0', '0', '0'};
index=0;
}
}
sb.append("\n");
}
return sb.toString();
}
private String fourByteBinary(String binaryStr){
int decimal = Integer.parseInt(binaryStr,2);
if (decimal>15){
return Integer.toString(decimal,16).toUpperCase();
} else {
return "0" + Integer.toString(decimal,16).toUpperCase();
}
}
private String encodeHexAscii(String code){
int maxlinea = widthBytes * 2;
StringBuffer sbCode = new StringBuffer();
StringBuffer sbLinea = new StringBuffer();
String previousLine = null;
int counter = 1;
char aux = code.charAt(0);
boolean firstChar = false;
for(int i = 1; i< code.length(); i++){
if(firstChar){
aux = code.charAt(i);
firstChar = false;
continue;
}
if(code.charAt(i)=='\n'){
if(counter>=maxlinea && aux=='0'){
sbLinea.append(",");
} else if(counter>=maxlinea && aux=='F'){
sbLinea.append("!");
} else if (counter>20){
int multi20 = (counter/20)*20;
int resto20 = (counter%20);
sbLinea.append(mapCode.get(multi20));
if(resto20!=0){
sbLinea.append(mapCode.get(resto20) + aux);
} else {
sbLinea.append(aux);
}
} else {
sbLinea.append(mapCode.get(counter) + aux);
if(mapCode.get(counter)==null){
}
}
counter = 1;
firstChar = true;
if(sbLinea.toString().equals(previousLine)){
sbCode.append(":");
} else {
sbCode.append(sbLinea.toString());
}
previousLine = sbLinea.toString();
sbLinea.setLength(0);
continue;
}
if(aux == code.charAt(i)){
counter++;
} else {
if(counter>20){
int multi20 = (counter/20)*20;
int mod20 = (counter%20);
sbLinea.append(mapCode.get(multi20));
if(mod20!=0){
sbLinea.append(mapCode.get(mod20) + aux);
} else {
sbLinea.append(aux);
}
} else {
sbLinea.append(mapCode.get(counter) + aux);
}
counter = 1;
aux = code.charAt(i);
}
}
return sbCode.toString();
}
private String headDoc(){
String str = "^XA " +
"^FO0,0^GFA,"+ total + ","+ total + "," + widthBytes +", ";
return str;
}
private String footDoc(){
String str = "^FS"+
"^XZ";
return str;
}
public void setCompressHex(boolean compressHex) {
this.compressHex = compressHex;
}
public void setBlacknessLimitPercentage(int percentage){
blackLimit = (percentage * 768/100);
}
public static void main(String[] args) throws Exception {
BufferedImage orginalImage = ImageIO.read(new File("/tmp/logo.jpg"));
ZPLConveter zp = new ZPLConveter();
zp.setCompressHex(true);
zp.setBlacknessLimitPercentage(50);
System.out.println(zp.convertfromImg(orginalImage));
}
}
+0
謝謝你的回答。我嘗試了您的代碼,但打印時圖像尺寸縮小了。我想這需要打印1/3。我正在嘗試打印寬度爲384像素和高度爲288像素4/3比例的圖像 – Lokesh 2017-09-13 03:10:58
相關問題
- 1. 如何使用C#通過USB將原始ZPL發送到斑馬打印機
- 2. 打印到斑馬打印機在php
- 3. 斑馬打印機SDK swift
- 4. 模仿斑馬打印機
- 5. 斑馬打印機:打印圖像模糊
- 6. 發送命令到遠程打印機
- 7. 如何在java中打印斑馬打印機中的波蘭語字符?
- 8. 從Java應用程序向斑馬打印機發送條形碼
- 9. 直接從java發送命令到打印機
- 10. 如何使用Java將原始數據發送到打印機
- 11. 如何使用命令行選項將文件發送到打印機?
- 12. 將命令直接發送到打印機的正確方法!
- 13. 通過打印服務器發送原始ZPL斑馬打印機工作不
- 14. 斑馬打印機:如何打印UTF-8特殊字符
- 15. 如何打印條碼斑馬打印機編程語言
- 16. Java打印機:將文檔發送到遠程打印機
- 17. 發送打印機特定命令
- 18. 使用串口發送ESC/POS命令到打印機
- 19. 斑馬打印機KR403端口號
- 20. 「無法初始化設備」將打印命令發送到共享打印機
- 21. 如何發送命令到USB打印機?
- 22. 斑馬GK420d ZPL打印機狀態通過USB在java中
- 23. C++發送圖像到打印機,(打印)
- 24. 如何每次發送打印命令
- 25. 將PNG包含在LP2844Z(斑馬打印機)收件箱中
- 26. 將打印命令發送給C#中的非默認打印機的打印機
- 27. 使用Python將打印作業發送到USB打印機
- 28. 如何通過.Net SDK將字體文件發送到斑馬打印機(MZ 220)?
- 29. 發送ESC命令到打印機的C#
- 30. Java打印服務API:將作業發送到打印機後臺打印?
Zebra打印機是不完全的主流技術測試的結果。你最好在你的問題中包含十六進制格式要求。 – 2013-03-01 11:34:26
以下是關於[zpl maual文檔](https://km.zebra.com/kb/index?page=content&id=SO6755)中十六進制格式的說明(您可以看到^ DG命令)。 – htwj1998 2013-03-03 02:29:33
我在這裏寫了一個在線轉換器http://www.jcgonzalez.com/zpl-ascii-hex-representation-image-generator – PbxMan 2016-06-03 07:44:10