Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中使用^DG命令将图像发送到zebra打印机?_Java_Image_Zebra Printers_Zpl - Fatal编程技术网

如何在java中使用^DG命令将图像发送到zebra打印机?

如何在java中使用^DG命令将图像发送到zebra打印机?,java,image,zebra-printers,zpl,Java,Image,Zebra Printers,Zpl,有人知道如何在java中将图像(如*.bmp文件)转换为十六进制字符串,以便在^DG命令(zpl中的一个命令)中使用十六进制字符串。谢谢 这不容易。您必须将图像转换为每像素1位b/w,然后计算包括每行字节数在内的大小,然后正确设置~DG命令的参数以存储它 或者 Zebra提供了将图像转换为GRF(Zebra内部图像类型)的API。软件包中有一系列开发人员演示,包括如何在打印机上打印/存储图像作为GRF的演示 希望这有帮助我已经编写了一个java示例,可以做到这一点。 您还可以创建ascii十六

有人知道如何在java中将图像(如*.bmp文件)转换为十六进制字符串,以便在^DG命令(zpl中的一个命令)中使用十六进制字符串。谢谢

这不容易。您必须将图像转换为每像素1位b/w,然后计算包括每行字节数在内的大小,然后正确设置
~DG
命令的参数以存储它

或者

Zebra提供了将图像转换为GRF(Zebra内部图像类型)的API。软件包中有一系列开发人员演示,包括如何在打印机上打印/存储图像作为GRF的演示


希望这有帮助

我已经编写了一个java示例,可以做到这一点。 您还可以创建ascii十六进制代码或压缩ZPL代码,并选择黑度百分比以提高精度

相应地更改主方法中的参数。需要更广泛的解释

*不在此处打印即可测试结果

导入java.awt.Graphics2D;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.Map;
导入javax.imageio.imageio;
公共级zplconverter{
私有整数黑限=380;
私人整数合计;
私有整数字节;
私有布尔压缩十六进制=false;
私有静态映射mapCode=newHashMap();
{
地图代号.put(1,“G”);
地图编码.put(2,“H”);
地图编号.put(3,“I”);
地图编号.put(4,“J”);
地图编码.put(5,“K”);
地图编号.put(6,“L”);
地图编号.put(7,“M”);
地图代号.put(8,“N”);
地图编号.put(9,“O”);
地图编码.put(10,“P”);
地图编码.put(11,“Q”);
地图编码.put(12,“R”);
地图编号.put(13,“S”);
地图编码.put(14,“T”);
地图编号.put(15,“U”);
地图编号.put(16,“V”);
地图编号.put(17,“W”);
地图代号.put(18,“X”);
地图编码.put(19,“Y”);
地图编码.put(20,“g”);
地图编码.put(40,“h”);
地图编号.put(60,“i”);
地图编码.put(80,“j”);
地图编码.put(100,“k”);
地图编码.put(120,“l”);
地图编码.put(140,“m”);
地图编码.put(160,“n”);
地图代号.put(180,“o”);
地图编码.put(200,“p”);
地图编码.put(220,“q”);
地图编码.put(240,“r”);
地图编号.put(260,“s”);
地图编码.put(280,“t”);
地图编码。put(300,“u”);
地图编码.put(320,“v”);
地图编码.put(340,“w”);
地图编码.put(360,“x”);
地图编码.put(380,“y”);
地图编码.put(400,“z”);
}
公共字符串convertfromImg(BuffereImage图像)引发IOException{
字符串body=createBody(图像);
if(压缩十六进制)
body=i(body);
返回headDoc()+body+footDoc();
}
私有字符串createBody(BuffereImage orginalImage)引发IOException{
StringBuffer sb=新的StringBuffer();
Graphics2D graphics=orginalImage.createGraphics();
graphics.drawImage(orginalImage,0,0,null);
int height=orginalImage.getHeight();
int width=orginalImage.getWidth();
int rgb,红色、绿色、蓝色,索引=0;
char auxBinaryChar[]={'0','0','0','0','0','0','0'};
宽度字节=宽度/8;
如果(宽度%8>0){
宽度字节=((int)(宽度/8))+1);
}否则{
宽度字节=宽度/8;
}
this.total=宽度字节*高度;
对于(inth=0;h16)&0x000000FF;
绿色=(rgb>>8)&0x000000FF;
蓝色=(rgb)&0x000000FF;
char auxChar='1';
int totalColor=红色+绿色+蓝色;
如果(totalColor>blackLimit){
auxChar='0';
}
auxBinaryChar[指数]=auxChar;
索引++;
如果(索引==8 | | w==(宽度-1)){
sb.追加(fourByteBinary(新字符串(auxBinaryChar));
auxBinaryChar=新字符[]{'0','0','0','0','0','0','0'};
指数=0;
}
}
某人附加(“\n”);
}
使某人返回字符串();
}
私有字符串fourByteBinary(字符串二进制str){
int decimal=Integer.parseInt(二进制str,2);
如果(十进制>15){
返回整数.toString(十进制,16).toUpperCase();
}否则{
返回“0”+整数.toString(十进制,16).toUpperCase();
}
}
私有字符串编码(字符串代码){
int maxlinea=widthBytes*2;
StringBuffer sbCode=新的StringBuffer();
StringBuffer sbLinea=新的StringBuffer();
字符串previousLine=null;
int计数器=1;
char aux=代码charAt(0);
布尔值firstChar=false;
对于(int i=1;i=maxlinea&&aux==0'){
sbLinea.追加(“,”);
}否则如果(计数器>=maxlinea&&aux==F'){
sbLinea.附加(“!”);
}否则,如果(计数器>20){
int multi20=(计数器/20)*20;
int resto20=(计数器%20);
sbLinea.append(mapCode.get(multi20));
如果(resto20!=0){
sbLinea.append(mapCode.get(resto20)+aux);
}否则{
sbLinea.append(辅助);
}
}否则{
sbLinea.append(mapCode.get(counter)+aux);
if(mapCode.get(计数器)==null){
}
}
计数器=1;
firstChar=true;
if(sbLinea.toString().equa
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));        
    }
}