📄 maxicode.java
字号:
// in mode A or B, 1 char C,D or E
if ((!done) && (this.charSet(data.charAt(pos),'C')>=0)) {
tmp[count++]=(byte) this.SHIFTC;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'C');
pos++;
done=true;
}
if ((!done) && (this.charSet(data.charAt(pos),'D')>=0)) {
tmp[count++]=(byte) this.SHIFTD;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'D');
pos++;
done=true;
}
if ((!done) && (this.charSet(data.charAt(pos),'E')>=0)) {
tmp[count++]=(byte) this.SHIFTE;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'E');
pos++;
done=true;
}
}
if ((currentSet>'B') && (!done)) {
// in mode C, D or E
// if now A
if ((!done) && (this.charSet(data.charAt(pos),'A')>=0)) {
tmp[count++]=(byte) this.LACHTA;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'A');
pos++;
done=true;
currentSet='A';
}
// if now B
if ((!done) && (this.charSet(data.charAt(pos),'B')>=0)) {
tmp[count++]=(byte) this.LACHTB;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'B');
pos++;
done=true;
currentSet='B';
}
// if more than 1 C , D or E
if (((l-pos)>=2) && (!done))
if ((this.charSet(data.charAt(pos),'C')>=0) &&
(this.charSet(data.charAt(pos+1),'C')>=0) && (currentSet!='C') )
{
if (currentSet!='C') {
tmp[count++]=(byte) this.SHIFTC;
tmp[count++]=(byte) this.LOCKC;
}
tmp[count++]=(byte) this.charSet(data.charAt(pos),'C');
pos++;
done=true;
currentSet='C';
}
if (((l-pos)>=2) && (!done))
if ((this.charSet(data.charAt(pos),'D')>=0) &&
(this.charSet(data.charAt(pos+1),'D')>=0) && (currentSet!='D'))
{
if (currentSet!='D') {
tmp[count++]=(byte) this.SHIFTD;
tmp[count++]=(byte) this.LOCKD;
}
tmp[count++]=(byte) this.charSet(data.charAt(pos),'D');
pos++;
done=true;
currentSet='D';
}
if (((l-pos)>=2) && (!done))
if ((this.charSet(data.charAt(pos),'E')>=0) &&
(this.charSet(data.charAt(pos+1),'E')>=0) && (currentSet!='E'))
{
if (currentSet!='E') {
tmp[count++]=(byte) this.SHIFTE;
tmp[count++]=(byte) this.LOCKE;
}
tmp[count++]=(byte) this.charSet(data.charAt(pos),'E');
pos++;
done=true;
currentSet='E';
}
// if only 1 C , D or E
if ((!done) && (this.charSet(data.charAt(pos),'C')>=0)) {
if (currentSet!='C') tmp[count++]=(byte) this.SHIFTC;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'C');
pos++;
done=true;
}
if ((!done) && (this.charSet(data.charAt(pos),'D')>=0)) {
if (currentSet!='D') tmp[count++]=(byte) this.SHIFTD;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'D');
pos++;
done=true;
}
if ((!done) && (this.charSet(data.charAt(pos),'E')>=0)) {
if (currentSet!='E') tmp[count++]=(byte) this.SHIFTE;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'E');
pos++;
done=true;
}
}
}
// latch to A or B
if (currentSet!='B') tmp[count++]=(byte) this.LACHTB;
else tmp[count++]=(byte) this.LACHTA_B;
for (int i= count;i<144;i++) tmp[count++]=(byte) this.PAD;
return tmp;
}
/** convert from bits to integer
*/
private byte parseBits(String vStr) {
int v=0;
if (vStr.charAt(0)=='1') v=v+32;
if (vStr.charAt(1)=='1') v=v+16;
if (vStr.charAt(2)=='1') v=v+8;
if (vStr.charAt(3)=='1') v=v+4;
if (vStr.charAt(4)=='1') v=v+2;
if (vStr.charAt(5)=='1') v=v+1;
return (byte) v;
}
/** encode 9 consecutive digits
*/
private byte[] encode9Digits(String s) {
int Int=0;
try {
Int=new Integer(s).intValue();
} catch (Exception e1) {Int=0;};
String tmp=getBits(Int,30);
byte[] data= new byte[5];
for (int i=0;i<5;i++) data[i]=parseBits(tmp.substring(i*6,(i+1)*6));
return data;
}
/**
Apply the Reed Solomon algorithm
*/
private void addReedSolomon(byte[] data) {
int[] primary=new int[144];
int[] secondaryEven=new int[144];
int[] secondaryOdd=new int[144];
reed6 RS=new reed6();
// extract primary
for (int i=0;i<10;i++) primary[i]=data[i];
for (int i=10;i<20;i++) primary[i]=0;
// RS for primary
RS.calcRS(primary,10,10);
// extract interleaved blocks
int j=0;
for (int i=0;i<124;i=i+2) {
secondaryEven[j]=data[20+i];
secondaryOdd[j]=data[20+i+1];
j++;
}
int k=42;
int t2=20;
if (mode ==5) {
k=34;
t2=28;
}
// calculate RS for interleaved blocks
RS.calcRS(secondaryEven,k,t2);
RS.calcRS(secondaryOdd,k,t2);
// move data to the final array
for (int i=0;i<20;i++) data[i]=(byte) primary[i];
j=20;
for (int i=0;i<62;i++) {
data[j]=(byte) secondaryEven[i];
j++;
data[j]=(byte) secondaryOdd[i];
j++;
}
}
/**
apply tilde
*/
private String applyTilde(String code) {
int i;
int c=0;
int longi=code.length();
String result="";
boolean done=false;
for (i=0;i<longi;i++) {
c=(int) code.charAt(i);
if (c=='~') {
// process special cases
if (i<(longi-1)) {
char nextc=code.charAt(i+1);
// quoting character
if (nextc=='~') {
result=result+'~';
i++;
} else {
// dNNN
if (i<(longi-3)) {
String ascString=code.substring(i+1,i+4);
int asc=0;
try {
asc= new Integer(ascString).intValue();
} catch (Exception e) {asc=0;}
if (asc>255) asc=255;
// the ascii value
result=result+(char) asc;
i=i+3;
}
} // not escape
} // we have two chars at least
} else {
// no tilde
result=result+(char) c;
}
}
return result;
}
/**
Set resolution of the printer (in dpi). The default is 200 dpi.
*/
public boolean setResolution(int r) {
resolution=r;
return setL(L);
}
/**
Get current resolution of the printer (in dpi)
*/
public int getResolution() { return resolution; }
/**
Set length of length symbol. The default is 25.5 millimeters.
*/
public boolean setL(double l) {
dpm=resolution/25; // dpi to dpm
r1=(int) java.lang.Math.round((0.51)*dpm);
r2=(int) java.lang.Math.round((1.18)*dpm);
r3=(int) java.lang.Math.round((1.86)*dpm);
r4=(int) java.lang.Math.round((2.53)*dpm);
r5=(int) java.lang.Math.round((3.20)*dpm);
r6=(int) java.lang.Math.round((3.87)*dpm);
L=l;
W=(L/29);
V=(2/java.lang.Math.sqrt(3))*W;
X=W;
Y=(1.5/java.lang.Math.sqrt(3))*W;
H=32*Y;
Lpixels=(int) (L*dpm);
Xpixels=(int) java.lang.Math.round(Lpixels/29);
Wpixels=Xpixels;
Vpixels=(int) java.lang.Math.round((2/java.lang.Math.sqrt(3))*Wpixels);
Ypixels=(int) java.lang.Math.round((1.5/java.lang.Math.sqrt(3))*Wpixels);
La=0;
Ha=0;
La=(Xpixels*29)/dpm;
Ha=(Ypixels*32)/dpm;
boolean inLimits=true;
if ((La<24) || (La>27)) inLimits=false;
if ((Ha<22.9) || (Ha>25.8)) inLimits=false;
if (!inLimits) {
// round Xpixels the other way
double tmpXpixels=Xpixels;
if (tmpXpixels>(Lpixels/29)) Xpixels--;
else Xpixels++;
Wpixels=Xpixels;
Vpixels=(int) java.lang.Math.round((2/java.lang.Math.sqrt(3))*Wpixels);
Ypixels=(int) java.lang.Math.round((1.5/java.lang.Math.sqrt(3))*Wpixels);
La=(Xpixels*29)/dpm;
Ha=(Ypixels*32)/dpm;
inLimits=true;
if ((La<24) || (La>27)) inLimits=false;
if ((Ha<22.9) || (Ha>25.8)) inLimits=false;
if (!inLimits) return false;
}
centerY=((int) (Vpixels/2))+(16*Ypixels);
centerX=((int) (Xpixels/2))+(14*Xpixels);
tolerancePixels=(int) java.lang.Math.round(tolerance*dpm);
this.prefW=this.Xpixels*30;
this.prefH=(this.Ypixels*32)+this.Vpixels;
//***************************************** demo
//***************************************** for non-demo rem out next 2 lines
//prefW=prefW+30;
//prefH=prefH+30;
return true;
}
private static int[][] layout;
/**
layout of the hexagons at 12 dpm
*/
int[][] pattern12dpm = {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,1,1,1,1,1,0,0},
{0,0,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,1,1},
{0,0,1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,1,1,1,1,1,1,1,1,1},
{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
{0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0}
};
/**
layout of the hexagons at 8 dpm
*/
int[][] pattern8dpm = {
{0,0,0,0,0,0,0},
{0,0,0,1,1,0,0},
{0,0,1,1,1,1,0},
{0,1,1,1,1,1,1},
{0,1,1,1,1,1,1},
{0,1,1,1,1,1,1},
{0,0,1,1,1,1,0},
{0,0,0,1,1,0,0},
};
/**
Constructor
*/
public MaxiCode() {
super();
//this.serviceClas="999";
//this.country="056";
//this.zipCode="999999999";
//this.mode=2;
//this.createPrimary();
setL(25.5);
if (layout==null) createLayout();
}
/**
convert from int to bit string
*/
private String getBits(int c,int num) {
String bits=Integer.toBinaryString(c);
if (bits.length()<num)
for (int i=bits.length();i<num;i++) bits="0"+bits;
else bits=bits.substring(bits.length()-num,bits.length());
return bits;
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -