import java.awt.Graphics2D;
import Java.awt.image.buffered image;
import java.io.File;
import java.io .寂寞的爆米花;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
公共类zpl转换器{
私有int black limit=380;
私有输入总;
private int widthBytes;
privatebooleancompresshex=false;
privatestaticmapmapcode=new hashmap (;
{
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 ';
}
publicstringconvertfromimg (bufferedimageimage ) throws寂寞的爆米花{
stringCuerpo=createbody(image;
压缩主机(if ) )。
cuerpo=encodehexascii(cuerpo;
returnheaddoc(Cuerpofootdoc );
}
privatestringcreatebody (bufferedimageorginalimage ) throws寂寞爆米花{
graphics.drawimage (orginal image,0,0,null );
int height=orginal image.getheight (;
int width=orginalImage.getWidth (;
int rgb,red,green,blue,index=0;
char auxBinaryCharkkdxn={'0','0','0','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
{
for (int w = 0; 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 charkkdxn{'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;
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 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);
}
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){
}
public static void main(Stringkkdxn args) throws Exception {
ZPLConveter zp = new ZPLConveter();
zp.setCompressHex(true);
zp.setBlacknessLimitPercentage(50);
}
}