📄 maxicode.java
字号:
create primary message in mode 2 and 3
*/
private byte[] createPrimary() {
byte[] data=new byte[20];
String bitStream="";
// switch to mode 3 if necesary
for (int i=0;i<this.zipCode.length();i++)
// postal code is not numeric, mode 3
if (!isDigit(this.zipCode.charAt(i))) this.mode=3;
// add spaces to zip code in mode 3
if ((this.mode==3) && (this.zipCode.length()<6))
for (int i=this.zipCode.length();i<6;i++) this.zipCode=this.zipCode+" ";
// encode postal code
String zipCodeBinary2="";
String zipCodeBinary3="";
String zipCodeLength2="";
for (int i=0;i<this.zipCode.length();i++)
// mode 3 only encodes up to 6 chars
if (i<6) zipCodeBinary3=zipCodeBinary3+getBits(this.charSet(this.zipCode.charAt(i),'A'),6);
if (this.mode==2) {
// mode 2 up to 9 digits
if (this.zipCode.length()>9) this.zipCode=this.zipCode.substring(0,9);
// encode class service
int zipCodeInt=0;
try {
zipCodeInt=new Integer(this.zipCode).intValue();
} catch (Exception e1) {zipCodeInt=0;};
zipCodeBinary2=getBits(zipCodeInt,30);
zipCodeLength2=getBits(this.zipCode.length(),6);
}
String modeBits="0011";
if (this.mode==2) modeBits="0010";
// encode class service
int service=0;
try {
service=new Integer(this.serviceClass).intValue();
} catch (Exception e1) {service=0;};
// encode country
int countryInt=0;
try {
countryInt=new Integer(this.country).intValue();
} catch (Exception e1) {countryInt=0;};
// add last 2 bits of the postcode to the first byte
if (mode==2) bitStream=getBits(service,10)+getBits(countryInt,10)+zipCodeLength2+zipCodeBinary2+modeBits;
else bitStream=getBits(service,10)+getBits(countryInt,10)+zipCodeBinary3+modeBits;
if (bitStream.length()!=60) {
System.out.println("Wrong bitStream in primary Message "+bitStream);
return data;
}
for (int j=0;j<10;j++) {
String vStr=bitStream.substring(j*6,(j+1)*6);
data[10-j-1]=parseBits(vStr);
}
return data;
}
private char getCharSet(int c) {
int i=0;
i=c-1;
while (i<this.table.length) {
if (table[i][0]==c)
// return charset
return (char) table[i][1];
// the index is too high
if (table[i][0]>c) break;
i++;
}
return ' ';
}
/**
returns true if the character is a digit
*/
private boolean isDigit(int c) {
return ((c>='0') && (c<='9'));
}
/**
returns value of the char (c) in the character set (cs), or -1 if not in the character set
*/
private int charSet(int c,char cs) {
int i=0;
i=c-1;
while (i<this.table.length) {
if (table[i][0]==c)
if (table[i][1]==cs) return table[i][2];
// the index is too high
if (table[i][0]>c) break;
i++;
}
return -1;
}
/**
extract postal code, country and service from the data
*/
private String extractPrimaryData(String data) {
char GS=29;
char RS=30;
int startPosition=0;
String year="";
if (data.substring(0,7).compareTo("[)>"+RS+"01"+GS)==0) {
startPosition=9;
}
else startPosition=0;
int element=0;
int i;
for (i=startPosition;i<data.length();i++) {
if (data.charAt(i)==GS) element++;
else {
if (element==0) this.zipCode=this.zipCode+data.charAt(i);
if (element==1) this.country=this.country+data.charAt(i);
if (element==2) this.serviceClass=this.serviceClass+data.charAt(i);
}
// exit, we have all three fields
if (element==3) break;
}
if (i<data.length()) data=data.substring(0,startPosition)+data.substring(i+1,data.length());
else data=data.substring(0,startPosition);
return data;
}
private void layoutHexagon(int x,int y,int c) {
int base=(c-1)*6;
layout[x][y]=base+2;
layout[x+1][y]=base+1;
layout[x][y+1]=base+4;
layout[x+1][y+1]=base+3;
layout[x][y+2]=base+6;
layout[x+1][y+2]=base+5;
}
private void layoutHexagon2(int x,int y,int c) {
int base=(c-1)*6;
layout[x][y]=base+1;
layout[x+1][y+1]=base+2;
layout[x][y+1]=base+3;
layout[x][y+2]=base+4;
layout[x+1][y+3]=base+5;
layout[x][y+3]=base+6;
}
private void hexagonList(int start,int end,int x,int y) {
int i=start;
int step=1;
if (end<start) step=-1;
while (i!=end) {
layoutHexagon(x,y,i);
x=x+2;
i=i+step;
}
}
/**
create a layout in the symbol
*/
private void createLayout() {
layout=new int[30][33];
hexagonList(21,35,0,0);
hexagonList(48,34,0,3);
hexagonList(49,63,0,6);
hexagonList(69,65,0,9);
hexagonList(70,73,0,12);
hexagonList(81,78,0,15);
hexagonList(82,85,0,18);
hexagonList(94,90,0,21);
hexagonList(95,109,0,24);
hexagonList(122,108,0,27);
hexagonList(123,137,0,30);
hexagonList(65,62,22,9);
hexagonList(73,76,22,12);
hexagonList(78,75,22,15);
hexagonList(85,88,22,18);
hexagonList(90,87,22,21);
layoutHexagon(8,9,14);
layoutHexagon(8,12,10);
layoutHexagon(6,12,18);
layoutHexagon(8,18,11);
layoutHexagon(6,18,17);
layoutHexagon(8,21,13);
layoutHexagon(20,9,19);
layoutHexagon(20,12,15);
layoutHexagon(20,18,16);
layoutHexagon(20,21,20);
layoutHexagon(18,21,12);
layoutHexagon2(28,1,137);
layoutHexagon2(28,5,138);
layoutHexagon2(28,9,139);
layoutHexagon2(28,13,140);
layoutHexagon2(28,17,141);
layoutHexagon2(28,21,142);
layoutHexagon2(28,25,143);
layoutHexagon2(28,29,144);
// back or white
layout[28][0]=-1;
layout[29][0]=-1;
layout[10][9]=-1;
layout[11][9]=-1;
layout[11][10]=-1;
layout[17][9]=0;
layout[17][9]=0;
layout[18][10]=0;
layout[7][15]=-1;
layout[7][16]=0;
layout[8][16]=-1;
layout[20][16]=-1;
layout[21][16]=0;
layout[20][17]=-1;
layout[10][22]=-1;
layout[11][22]=0;
layout[10][23]=-1;
layout[17][22]=-1;
layout[16][23]=0;
layout[17][23]=-1;
// other
layout[12][9]=14;
layout[13][9]=13;
layout[14][9]=38;
layout[15][9]=37;
layout[16][9]=3;
layout[18][9]=45;
layout[19][9]=44;
layout[10][10]=41;
layout[12][10]=16;
layout[13][10]=15;
layout[14][10]=40;
layout[15][10]=39;
layout[16][10]=4;
layout[19][10]=46;
layout[10][11]=42;
layout[16][11]=6;
layout[17][11]=5;
layout[18][11]=48;
layout[19][11]=47;
layout[10][12]=17;
layout[18][12]=21;
layout[19][12]=20;
layout[18][13]=23;
layout[19][13]=22;
layout[19][14]=24;
layout[6][15]=49;
layout[8][15]=31;
layout[19][15]=1;
layout[20][15]=54;
layout[21][15]=53;
layout[6][16]=50;
layout[6][17]=52;
layout[7][17]=51;
layout[8][17]=32;
layout[19][17]=2;
layout[21][17]=43;
layout[19][18]=27;
layout[18][19]=29;
layout[19][19]=28;
layout[10][20]=18;
layout[18][20]=19;
layout[19][20]=30;
layout[10][21]=33;
layout[17][21]=11;
layout[12][22]=8;
layout[13][22]=7;
layout[14][22]=36;
layout[15][22]=35;
layout[16][22]=12;
layout[11][23]=34;
layout[12][23]=10;
layout[13][23]=9;
layout[14][23]=26;
layout[15][23]=25;
}
/** paints a module
*/
private void paintModule(Graphics g2,int x,int y,int[][] pattern,java.awt.Color c) {
int w=pattern[0].length;
int h=pattern.length;
g2.setColor(c);
for (int i=0;i<w;i++)
for (int j=0;j<h;j++) {
if (pattern[j][i]==1) g2.drawLine(x+i,y+j,x+i,y+j);
}
}
/**
creates a hexagon pattern, if a standard cannot be used.
*/
public int[][] createPattern() {
int[][] p=new int[Vpixels][Xpixels];
int[][] ptmp=new int[Vpixels-tolerancePixels][Xpixels-tolerancePixels];
// arrays reset
for (int i=0;i<p[0].length;i++)
for (int j=0;j<p.length;j++)
p[j][i]=0;
for (int i=0;i<ptmp[0].length;i++)
for (int j=0;j<ptmp.length;j++)
ptmp[j][i]=1;
int overlap=Vpixels-Ypixels;
int hexagonTopSide=(int) java.lang.Math.floor(ptmp[0].length/3);
int pixelsToRemove=(ptmp[0].length-hexagonTopSide)/2;
// set to 0 the corners
for (int i=0;i<pixelsToRemove;i++)
for (int j=0;j<(pixelsToRemove-i);j++) {
ptmp[j][i]=0; // left top
ptmp[j][ptmp[0].length-1-i]=0; // right top
ptmp[ptmp.length-j-1][i]=0; // left bottom
ptmp[ptmp.length-j-1][ptmp[0].length-1-i]=0; // right bottom
}
for (int i=0;i<ptmp[0].length;i++)
for (int j=0;j<ptmp.length;j++)
p[j+tolerancePixels][i+tolerancePixels]=ptmp[j][i];
// for debug only
//String line="";
//for (int j=0;j<Vpixels;j++) {
// line="";
// for (int i=0;i<Xpixels;i++){
// line=line+p[j][i]+" ";
// }
// System.out.println(line);
//}
return p;
}
/**
force recalculating the symbol next time paint() is called
*/
public void setRedraw(boolean b) {
redraw=b;
pattern=null;
}
/** paints the barcode and rotates it if necessary
*/
/**
paints the maxicode symbol.
*/
public void paint(Graphics g) {
if (tmpImage==null) redraw=true;
if (redraw)
{
redraw=false;
try
{
// this will only return an image is the component is being shown in the screen
tmpImage=this.createImage(prefW,prefH);
}
catch (Exception e) {tmpImage=null;};
java.awt.Graphics g2=g;
if (tmpImage!=null) g2=tmpImage.getGraphics();
// use pattern specified by user
if (pattern==null) pattern=this.userPattern;
// use the 2 hardcoded patterns
if ((Wpixels==7) && (Vpixels==8) && (pattern==null)) pattern=this.pattern8dpm;
if ((Wpixels==10) && (Vpixels==12) && (pattern==null)) pattern=this.pattern12dpm;
if ((Wpixels==14) && (Vpixels==16) && (pattern==null)) pattern=this.pattern16dpm;
if ((Wpixels==17) && (Vpixels==20) && (pattern==null)) pattern=this.pattern20dpm;
if ((Wpixels==20) && (Vpixels==24) && (pattern==null)) pattern=this.pattern24dpm;
// create new pattern
if (pattern==null) pattern=this.createPattern();
g2.setColor(this.color);
if (pattern==null) pattern=this.createPattern();
g2.setColor(this.backColor);
g2.fillRect(0,0,prefW,prefH);
g2.fillRect(0,0,this.getSize().width,this.getSize().height);
//***************************************** demo
//***************************************** demo next 3 lines
// demo
// g2.setColor(this.color);
// g2.drawString("IDAUTOMATION.COM DEMO",20,prefH-20);
// draw finder pattern
g2.setColor(this.color);
g2.fillArc(centerX-r6,centerY-r6,r6*2,r6*2,0,360);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -