package person.wangchen11.qrcode;
/**
* java 二维码生成工具
*
* @author wangchen11,望尘11
* q1012371864
*/
import java.util.LinkedList;
import person.wangchen11.rscode.RS;
public class QRCode {
private int mSize;//尺寸
private boolean []mData;
private boolean []mIsData;//用于标示是否为数据区
public final static boolean BLACK_BLOCK=true;
public final static boolean WHITE_BLOCK=!BLACK_BLOCK;
public final static boolean IS_NOT_DATA=true;
public final static boolean IS_DATA=!IS_NOT_DATA;
public final static byte MODE_ECI=0x7;//0b0111
public final static byte MODE_NUMERIC=0x1;//0b0001
public final static byte MODE_ALPHANUMERIC=0x2;//0b0010
public final static byte MODE_8BIT_BYTE=0x4;//0100
public final static byte MODE_KANJI=0x8;//01000
public final static byte MODE_STRUCTURED_APPEND=0x3;//0b0011
public final static byte MODE_FNC1=0x5;//0b0101(first postion)
public final static byte MODE_FNC2=0x9;//0b1001(second postion)
public final static byte MODE_TERMINATOR=0x0;//0b0000(end of message)
public final static byte ERROR_CORRECTION_LEVEL_L= 0;
public final static byte ERROR_CORRECTION_LEVEL_M= 1;
public final static byte ERROR_CORRECTION_LEVEL_Q= 2;
public final static byte ERROR_CORRECTION_LEVEL_H= 3;
private final static boolean PositionDetectionMap[][] =new boolean[][]{
{BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK},
{BLACK_BLOCK,WHITE_BLOCK,WHITE_BLOCK,WHITE_BLOCK,WHITE_BLOCK,WHITE_BLOCK,BLACK_BLOCK},
{BLACK_BLOCK,WHITE_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,WHITE_BLOCK,BLACK_BLOCK},
{BLACK_BLOCK,WHITE_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,WHITE_BLOCK,BLACK_BLOCK},
{BLACK_BLOCK,WHITE_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,WHITE_BLOCK,BLACK_BLOCK},
{BLACK_BLOCK,WHITE_BLOCK,WHITE_BLOCK,WHITE_BLOCK,WHITE_BLOCK,WHITE_BLOCK,BLACK_BLOCK},
{BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK},
};
private final static boolean AlignmentPatternMap[][] =new boolean[][]{
{BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK},
{BLACK_BLOCK,WHITE_BLOCK,WHITE_BLOCK,WHITE_BLOCK,BLACK_BLOCK},
{BLACK_BLOCK,WHITE_BLOCK,BLACK_BLOCK,WHITE_BLOCK,BLACK_BLOCK},
{BLACK_BLOCK,WHITE_BLOCK,WHITE_BLOCK,WHITE_BLOCK,BLACK_BLOCK},
{BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK,BLACK_BLOCK},
};
public int getVersion()
{
return (mSize-21)/4+1;
}
public int getSize()
{
return mSize;
}
public boolean isBlack(int x,int y)
{
return mData[x+y*mSize]==BLACK_BLOCK;
}
/**
*自动选用版本编码字符串
*目前 仅实现了字节流模式的编码
*
*@data 待编码数据流
*@dataLength 待编码数据流长度
*@errCodeLevel 纠错等级
*@maskPattern [0,7]选用什么掩码
*@mode 编码模式,暂未使用 ,请填 0
*/
public static QRCode autoEncodeString(byte []data,int dataLength,int errCodeLevel,int maskPattern,int mode)
{
int i;
QRCode code=null;
//[1,40]版本号中选择最小且能用的
for(i=1;i<=40;i++)
{
code=encodeString(data,dataLength,i,errCodeLevel,maskPattern,mode);
if(code!=null)
return code;
}
return null;
}
/**
*编码字符串
*目前 仅实现了字节流模式的编码
*
*@data 待编码数据流
*@dataLength 待编码数据流长度
*@version 版本号[1,40] 版本号越大生成的图片规格也越大
*@errCodeLevel 纠错等级
*@maskPattern [0,7]选用什么掩码
*@mode 编码模式,暂未使用 ,请填 0
*/
public static QRCode encodeString(byte []data,int dataLength,int version,int errCodeLevel,int maskPattern,int mode)
{
byte []encodedData=null;
QRCode code=null;
if(dataLength<=0)
return null;
encodedData=encodeData(data, version, errCodeLevel,mode);
if(encodedData==null)
return null;
code=createEmptyQRCode(version,errCodeLevel,maskPattern);
code.drawData(encodedData,encodedData.length);
code.maskData(maskPattern);
return code;
}
public static QRCode createEmptyQRCode(int version,int errorLevel,int maskPattern)
{
QRCode code=null;
code=new QRCode();
code.mSize=getSizeByVersion(version);
code.mData=new boolean[(code.mSize*code.mSize)];
code.mIsData=new boolean[(code.mSize*code.mSize)];
RS.memset(code.mData,WHITE_BLOCK,code.mSize*code.mSize);
RS.memset(code.mIsData,IS_DATA,code.mSize*code.mSize);
//System.out.println("new :");
//code.printQRCode();
code.drawPositionDetections();
code.drawTimingPatterns();
code.drawDarkModule();
code.drawUnusedSpace();
//System.out.println("drawUnusedSpace :");
//code.printQRCode();
code.drawAlignmentPatterns();
code.drawFormatInformation(errorLevel,maskPattern);
code.drawVersionInfomation(version);
//System.out.println("drawVersionInfomation :");
//code.printQRCode();
return code;
}
/**
*画所有的定位发现框
*/
void drawPositionDetections()
{
drawPositionDetection(0,0);
drawPositionDetection(this.mSize-7,0);
drawPositionDetection(0,this.mSize-7);
}
/**
*画定位发现框
*/
void drawPositionDetection(int xPostion,int yPostion)
{
int i;
int j;
int pos;
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
pos= xPostion+j+(yPostion+i)*this.mSize;
this.mData[pos] = PositionDetectionMap[i][j];
this.mIsData[pos] = IS_NOT_DATA;
}
}
}
void drawAlignmentPattern(int xPostion,int yPostion)
{
int i;
int j;
int pos;
if( (xPostion<=8&&yPostion<=8)||(xPostion<=8&&yPostion+4>=mSize-9)||(xPostion+4>=mSize-9&&yPostion<=8) )
{
return ;
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
pos=(xPostion+j)+(yPostion+i)*this.mSize;
this.mData[pos]=AlignmentPatternMap[i][j];
this.mIsData[pos]=IS_NOT_DATA;
}
}
}
int alignmentPattern[][] =new int[][] {
{ 0, 0},
{ 0, 0}, {18, 0}, {22, 0}, {26, 0}, {30, 0}, // 1- 5
{34, 0}, {22, 38}, {24, 42}, {26, 46}, {28, 50}, // 6-10
{30, 54}, {32, 58}, {34, 62}, {26, 46}, {26, 48}, //11-15
{26, 50}, {30, 54}, {30, 56}, {30, 58}, {34, 62}, //16-20
{28, 50}, {26, 50}, {30, 54}, {28, 54}, {32, 58}, //21-25
{30, 58}, {34, 62}, {26, 50}, {30, 54}, {26, 52}, //26-30
{30, 56}, {34, 60}, {30, 58}, {34, 62}, {30, 54}, //31-35
{24, 50}, {28, 54}, {32, 58}, {26, 54}, {30, 58}, //35-40
};
/**
*画矫正框
*/
void drawAlignmentPatterns()
{
int i,j;
int version=(this.mSize-21)/4+1;
if(alignmentPattern[ version ][0]<=0)
return ;
for(i=6;i<this.mSize-3;)
{
for(j=6;j<this.mSize-3;)
{
drawAlignmentPattern(j-2,i-2);
if(j>=alignmentPattern[ version ][0])
{
if(alignmentPattern[ version ][1]<=0)
break;
j+=(alignmentPattern[ version ][1]-alignmentPattern[ version ][0]);
}
else
j+=(alignmentPattern[ version ][0]-6);
}
if(i>=alignmentPattern[ version ][0])
{
if(alignmentPattern[ version ][1]<=0)
break;
i+=(alignmentPattern[ version ][1]-alignmentPattern[ version ][0]);
}
else
i+=(alignmentPattern[ version ][0]-6);
}
}
/**
*画校校正线
*/
void drawTimingPatterns()
{
int i;
int pos;
//y=6
for(i=7;i<this.mSize-7;i++)
{
pos=(6)*this.mSize+i;
if(i%2==1)
{
this.mData[pos]=WHITE_BLOCK;
}
else
{
this.mData[pos]=BLACK_BLOCK;
}
this.mIsData[pos] = IS_NOT_DATA;
}
//x=6
for(i=7;i<this.mSize-7;i++)
{
pos=i*this.mSize+6;
if(i%2==1)
{
this.mData[pos]=WHITE_BLOCK;
}
else
{
this.mData[pos]=BLACK_BLOCK;
}
this.mIsData[pos] = IS_NOT_DATA;
}
}
void drawDarkModule()
{
int pos;
pos=(this.mSize-8)*this.mSize+8;
this.mData[pos]=BLACK_BLOCK;
this.mIsData[pos] = IS_NOT_DATA;
}
/**
*画格式信息
*@errorLevel 0,1,2,3 2bit &0b 11 <<13
*@maskPattern 8个mask可供选择 3bit &0b 111 <<10
*@bch 校验码 10bit &0b 11 1111
没有合适的资源?快使用搜索试试~ 我知道了~
java,二维码生成算法
共24个文件
class:11个
java:9个
classpath:1个
需积分: 31 14 下载量 153 浏览量
2016-02-16
18:56:11
上传
评论
收藏 284KB ZIP 举报
温馨提示
开发环境:ecplice jdk:>=1.5 语言:java 功能:二维码生成。 这是我自己根据二维码生成算法的描述写的代码,纯java不需要依赖dll等。 对二维码生成算法有兴趣的童鞋可以下载学习一下,顺便给我点积分。嘿嘿。
资源推荐
资源详情
资源评论
收起资源包目录
QRCode20160216.zip (24个子文件)
QRCode
.project 382B
src
person
wangchen11
rscode
Main.java 3KB
RS.java 5KB
qrcode
QRFormatInfo.java 626B
QRSpec.java 3KB
QRVersionPattern.java 627B
QRCode.java 18KB
QREccSpec.java 3KB
Main.java 662B
QRCodeGUI.java 2KB
.settings
org.eclipse.jdt.core.prefs 598B
截图.jpg 3MB
.classpath 299B
bin
QRCodeGUI.class 2KB
person
wangchen11
rscode
Main.class 1KB
RS.class 3KB
qrcode
QRSpec.class 3KB
QRFormatInfo.class 813B
Main.class 1KB
QRVersionPattern.class 955B
QREccSpec.class 5KB
Block.class 644B
QRCode.class 12KB
QRCodeView.class 1KB
共 24 条
- 1
资源评论
w1012371864
- 粉丝: 3
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功