📄 maxicode.java
字号:
//*****************************************************************
//
// JAVA Source for com.idautomation.maxicode; 4.10
//
// Copyright, IDAutomation.com, Inc. 2000-2004.
// All rights reserved.
//
// http://www.IDAutomation.com/java/
//
// NOTICE:
// You may incorporate our Source Code in your application
// only if you own a valid Java Developer License
// from IDAutomation.com, Inc. and the copyright notices
// are not removed from the source code.
//
//*****************************************************************
package com.idautomation.barcode.maxicode;
import java.awt.*;
import java.awt.image.*;
/** this class paints a maxicode symbol
*/
public class MaxiCode extends Canvas
{
public java.awt.Color color=java.awt.Color.black;
public java.awt.Color backColor=java.awt.Color.white;
/**
length in mm of the symbol
*/
public double L=0; // mm
/**
resolution of the printer in dpi, Default is 200.
*/
private int resolution = 200; //dpi
double dpm=8;
double W=0; //mm
double V=0;//mm
double X=0;//mm
double Y=0;//mm
double H=0;//mm
double tolerance=0.12;
private int Wpixels=0; // pixel
private int Vpixels=0; // pixel
int Xpixels=0; // pixel
int Ypixels=0; // pixel
int Lpixels=0;
int tolerancePixels=0;
/**
paint the symbol at these coordinates
*/
public int offsetX =0;
/**
paint the symbol at these coordinates
*/
public int offsetY =0;
int r1=0; // pixel
int r2=0; // pixel
int r3=0; // pixel
int r4=0; // pixel
int r5=0; // pixel
int r6=0; // pixel
int rWidth=0;
int centerX; // pixel
int centerY; // pixel
//****************************************
//****************************************1=sale
//****************************************3=demo
protected final static int d=3;
// accepted size after rounding to pixels
private double La=0; // mm
private double Ha=0;
// current pattern for hexagons
/**
pattern used to print a hexagon. If null the class will calculate the most appropiate pattern
*/
private int[][] userPattern=null;
private int[][] pattern=null;
// temporary image
java.awt.Image tmpImage=null;
/** maxicode mode: Values range from 2 to 6; default=2 for US; 3 = International and 4 = standard (non-shipping) symbols.
*/ private int mode=2;
// fields for primary structure
/** postal code (modes 2 and 3) In mode 3 it can be an alphanumeric of length 5. In mode 2 can be a numeric of length 9.
*/
private String zipCode="";
/** service class (modes 2 and 3).
*/
private String serviceClass="";
/** country code (modes 2 and 3)
*/
private String country="";
/** data to be encoded. In modes 2 and 3 if the postal code is empty it will be extracted from the begining of this string (see maxicode specifications)
*/
private String dataStr="NO DATA";
// structured appended
/** number of symbol in structured appended mode
*/
private int numberOfCodes=1;
/** position of this symbol in the secuence of the structured appended
*/
private int positionOfCode=1;
private boolean redraw=true;
private int PAD=33;
private int ECI=27;
private int NSHIFT=31;
private int SHIFTA=59;
private int SHIFTB=59;
private int SHIFTC=60;
private int LOCKC=60;
private int SHIFTD=61;
private int LOCKD=61;
private int SHIFTE=62;
private int LOCKE=62;
private int LACHTB=63;
private int LACHTA_B=63; // when in mode B
private int LACHTA=58;
private int PADC=55;
private int PADE=29;
private int SHIFT2A=56;
private int SHIFT3A=57;
private char currentSet='A';
/**
* rotation angle. It can be 0,90,180 or 270
*/
private int rotation=0;
private boolean processTilde=true;
private int prefW=0;
private int prefH=0;
private boolean autoResize=false;
public boolean getProcessTilde() {return processTilde;}
public void setProcessTilde(boolean pt) {processTilde=pt;}
/** set a user defined pattern for the hexagons. For example:
* int[][] pattern = {
* {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},
* };
* myMaxicode.setPattern(pattern);
*
*/
public void setHexagonPattern(int[][] p) {
this.userPattern=p;
}
/**
* Returns "MaxiCode".
*/
public String getName()
{
return "MaxiCode";
}
/** width of hexagon modules (pixels)
*/
public int getWHexagon() {
return Wpixels;
}
/** Height of hexagon modules (pixels)
*/
public int getHHexagon() {
return Vpixels;
}
/** maxicode mode. Values range from 2 to 6
*/ public void setMode(int m) {
mode =m;
redraw=true;
// invalidate();
}
/** maxicode mode. Values range from 2 to 6
*/
public int getMode() {
return mode;
}
/** postal code (modes 2 and 3)
*/
public String getZipCode() {
return zipCode;
}
/** postal code (modes 2 and 3)
*/
public void setZipCode(String zp) {
zipCode=zp;
redraw=true;
}
/** service class (modes 2 and 3).
*/
public String getServiceClass() {
return serviceClass;
}
/** service class (modes 2 and 3).
*/
public void setServiceClass(String s) {
serviceClass=s;
redraw=true;
}
/** country code (modes 2 and 3)
*/
public String getCountry() {
return country;
}
/** country code (modes 2 and 3)
*/
public void setCountry(String c) {
country=c;
redraw=true;
}
/** data to be encoded. In modes 2 and 3 if the postal code is empty it will be extracted from the begining of this string (see maxicode specifications)
*/
public String getData() {
return dataStr;
}
/** data to be encoded. In modes 2 and 3 if the postal code is empty it will be extracted from the begining of this string (see maxicode specifications)
*/
public void setData(String d) {
dataStr=d;
redraw=true;
// invalidate();
}
/** number of symbol in structured appended mode
*/
public int getNumberOfCodes() {
return numberOfCodes;
}
/** number of symbol in structured appended mode
*/
public void setNumberOfCodes(int n) {
numberOfCodes=n;
redraw=true;
}
/** position of this symbol in the secuence of the structured appended
*/
/** number of symbol in structured appended mode
*/
public int getPositionOfCode() {
return positionOfCode;
}
/** number of symbol in structured appended mode
*/
public void setPositionOfCode(int n) {
positionOfCode=n;
// invalidate();
}
/**
* rotation angle. It can be 0,90,180 or 270
*/
public int getRotation() {
return rotation;
}
/**
* rotation angle. It can be 0,90,180 or 270
*/
public void setRotation(int r) {
rotation=r;
}
/** returns size needed to draw the symbol
*/
public Dimension getPreferredSize() {
return new Dimension(this.prefW,prefH);
}
/**
* if true the image will be resized to fit the size available for the component. This is used to show the symbol on the screen.
*/
public void setAutoResize(boolean b) {
autoResize=b;
}
/**
* if true the image will be resized to fit the size of the barcode
*/
public boolean getAutoResize() {
return autoResize;
}
// are the next "count" chars in "set"
private boolean nextIn(String data,int pos,int count, char set) {
for (int i=pos;i<pos+count;i++)
if (this.charSet(data.charAt(i),set)==-1) return false;
return true;
}
/** encodes a string in the maxicode character set
*/
private byte[] encode(String data) {
byte[] tmp=new byte[144];
int count=0;
int pos=0;
int l=data.length();
currentSet='A';
while ((pos<l) && (count<144)) {
boolean done=false;
// 9 digits?
if ((l-pos)>=9)
if (isDigit(data.charAt(pos)) &&
isDigit(data.charAt(pos+1)) &&
isDigit(data.charAt(pos+2)) &&
isDigit(data.charAt(pos+3)) &&
isDigit(data.charAt(pos+4)) &&
isDigit(data.charAt(pos+5)) &&
isDigit(data.charAt(pos+6)) &&
isDigit(data.charAt(pos+7)) &&
isDigit(data.charAt(pos+8)) )
{
byte[] digits = encode9Digits(data.substring(pos,pos+9));
tmp[count++]=(byte) this.NSHIFT;
for (int i=0;i<5;i++) tmp[count++]=digits[i];
done=true;
pos=pos+9;
}
// MODE B
if ((currentSet=='B') && (!done)) {
if ((!done) && (this.charSet(data.charAt(pos),'B')>=0)) {
tmp[count++]=(byte) this.charSet(data.charAt(pos),'B');
pos++;
done=true;
}
// in mode B, 4 chars A
if (((l-pos)>=4) && (!done))
if ((this.charSet(data.charAt(pos),'A')>=0) &&
(this.charSet(data.charAt(pos+1),'A')>=0) &&
(this.charSet(data.charAt(pos+2),'A')>=0) &&
(this.charSet(data.charAt(pos+3),'A')>=0) )
{
tmp[count++]= (byte) this.LACHTA_B;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'A');
pos++;
done=true;
currentSet='A';
}
// in mode B, 3 chars A
if (((l-pos)>=3) && (!done))
if ((this.charSet(data.charAt(pos),'A')>=0) &&
(this.charSet(data.charAt(pos+1),'A')>=0) &&
(this.charSet(data.charAt(pos+2),'A')>=0) )
{
tmp[count++]=(byte) this.SHIFT3A;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'A');
tmp[count++]=(byte) this.charSet(data.charAt(pos+1),'A');
tmp[count++]=(byte) this.charSet(data.charAt(pos+2),'A');
pos++;
pos++;
pos++;
done=true;
}
// in mode B, 2 chars A
if (((l-pos)>=2) && (!done))
if ((this.charSet(data.charAt(pos),'A')>=0) &&
(this.charSet(data.charAt(pos+1),'A')>=0) )
{
tmp[count++]= (byte) this.SHIFT2A;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'A');
tmp[count++]=(byte) this.charSet(data.charAt(pos+1),'A');
pos++;
pos++;
done=true;
}
// in mode B, 1 char A
if ((!done) && (this.charSet(data.charAt(pos),'A')>=0)) {
tmp[count++]=(byte) this.SHIFTA;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'A');
pos++;
done=true;
}
}
// MODE A
if ((currentSet=='A') && (!done)) {
if ((!done) && (this.charSet(data.charAt(pos),'A')>=0)) {
tmp[count++]=(byte) this.charSet(data.charAt(pos),'A');
pos++;
done=true;
}
// in mode A , 2 chars B
if (((l-pos)>=2) && (!done))
if ((this.charSet(data.charAt(pos),'B')>=0) &&
(this.charSet(data.charAt(pos+1),'B')>=0) && (this.charSet(data.charAt(pos+1),'A')==-1))
{
tmp[count++]=(byte) this.LACHTB;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'B');
pos++;
done=true;
currentSet='B';
}
// in mode A , 1 chars B
if ((!done) && (this.charSet(data.charAt(pos),'B')>=0)) {
tmp[count++]=(byte) this.SHIFTB;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'B');
pos++;
done=true;
}
}
if ((currentSet<='B') && (!done)) {
// in mode A or B, 4 chars C,D or E
if (((l-pos)>=4) && (!done))
if ((this.charSet(data.charAt(pos),'C')>=0) &&
(this.charSet(data.charAt(pos+1),'C')>=0) &&
(this.charSet(data.charAt(pos+2),'C')>=0) &&
(this.charSet(data.charAt(pos+3),'C')>=0) )
{
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)>=4) && (!done))
if ((this.charSet(data.charAt(pos),'D')>=0) &&
(this.charSet(data.charAt(pos+1),'D')>=0) &&
(this.charSet(data.charAt(pos+2),'D')>=0) &&
(this.charSet(data.charAt(pos+3),'D')>=0) )
{
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)>=4) && (!done))
if ((this.charSet(data.charAt(pos),'E')>=0) &&
(this.charSet(data.charAt(pos+1),'E')>=0) &&
(this.charSet(data.charAt(pos+2),'E')>=0) &&
(this.charSet(data.charAt(pos+3),'E')>=0) )
{
tmp[count++]=(byte) this.SHIFTE;
tmp[count++]=(byte) this.LOCKE;
tmp[count++]=(byte) this.charSet(data.charAt(pos),'E');
pos++;
done=true;
currentSet='E';
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -