/* 自定义生成QR Code的函数 */
public void AndroidQREncode(String strEncoding, int qrcodeVersion)
{
try
{
/* 建构QRCode编码对象 */
com.swetake.util.Qrcode testQrcode = new com.swetake.util.Qrcode();
/* L','M','Q','H' */
testQrcode.setQrcodeErrorCorrect('M');
/* "N","A" or other */
testQrcode.setQrcodeEncodeMode('B');
/* 0-20 */
testQrcode.setQrcodeVersion(qrcodeVersion);
// getBytes
byte[] bytesEncoding = strEncoding.getBytes("utf-8");
if (bytesEncoding.length>0 && bytesEncoding.length <120)
{
/* 将字符串透过calQrcode函数转换成boolean数组 */
boolean[][] bEncoding = testQrcode.calQrcode(bytesEncoding);
/* 依据编码后的boolean数组,绘图 */
drawQRCode(bEncoding, getResources().getColor(R.drawable.black));
}
}
catch (Exception e)
{
Log.i("HIPPO", Integer.toString(mEditText01.getText().length()) );
e.printStackTrace();
}
}
/* 在SurfaceView上绘制QRCode图片 */
private void drawQRCode(boolean[][] bRect, int colorFill)
{
/* test Canvas*/
int intPadding = 20;
/* 欲在SurfaceView上绘图,需先lock锁定SurfaceHolder */
Canvas mCanvas01 = mSurfaceHolder01.lockCanvas();
/* 设定画布绘制颜色 */
mCanvas01.drawColor(getResources().getColor(R.drawable.white));
/* 建立画笔 */
Paint mPaint01 = new Paint();
/* 设定画笔颜色及样式 */
mPaint01.setStyle(Paint.Style.FILL);
mPaint01.setColor(colorFill);
mPaint01.setStrokeWidth(1.0F);
/* 逐一加载2维boolean数组 */
for (int i=0;i<bRect.length;i++)
{
for (int j=0;j<bRect.length;j++)
{
if (bRect[j][i])
{
/* 依据数组值,绘出条形码方块 */
mCanvas01.drawRect(new Rect(intPadding+j*3+2, intPadding+i*3+2, intPadding+j*3+2+3, intPadding+i*3+2+3), mPaint01);
}
}
}
mSurfaceHolder01.unlockCanvasAndPost(mCanvas01);
}
public void AndroidQREncode(String strEncoding, int qrcodeVersion)
{
try
{
/* 建构QRCode编码对象 */
com.swetake.util.Qrcode testQrcode = new com.swetake.util.Qrcode();
/* L','M','Q','H' */
testQrcode.setQrcodeErrorCorrect('M');
/* "N","A" or other */
testQrcode.setQrcodeEncodeMode('B');
/* 0-20 */
testQrcode.setQrcodeVersion(qrcodeVersion);
// getBytes
byte[] bytesEncoding = strEncoding.getBytes("utf-8");
if (bytesEncoding.length>0 && bytesEncoding.length <120)
{
/* 将字符串透过calQrcode函数转换成boolean数组 */
boolean[][] bEncoding = testQrcode.calQrcode(bytesEncoding);
/* 依据编码后的boolean数组,绘图 */
drawQRCode(bEncoding, getResources().getColor(R.drawable.black));
}
}
catch (Exception e)
{
Log.i("HIPPO", Integer.toString(mEditText01.getText().length()) );
e.printStackTrace();
}
}
/* 在SurfaceView上绘制QRCode图片 */
private void drawQRCode(boolean[][] bRect, int colorFill)
{
/* test Canvas*/
int intPadding = 20;
/* 欲在SurfaceView上绘图,需先lock锁定SurfaceHolder */
Canvas mCanvas01 = mSurfaceHolder01.lockCanvas();
/* 设定画布绘制颜色 */
mCanvas01.drawColor(getResources().getColor(R.drawable.white));
/* 建立画笔 */
Paint mPaint01 = new Paint();
/* 设定画笔颜色及样式 */
mPaint01.setStyle(Paint.Style.FILL);
mPaint01.setColor(colorFill);
mPaint01.setStrokeWidth(1.0F);
/* 逐一加载2维boolean数组 */
for (int i=0;i<bRect.length;i++)
{
for (int j=0;j<bRect.length;j++)
{
if (bRect[j][i])
{
/* 依据数组值,绘出条形码方块 */
mCanvas01.drawRect(new Rect(intPadding+j*3+2, intPadding+i*3+2, intPadding+j*3+2+3, intPadding+i*3+2+3), mPaint01);
}
}
}
mSurfaceHolder01.unlockCanvasAndPost(mCanvas01);
}