http://yuncode.net/code/c_508664ed6306652
[java]代码库
import java.awt.Graphics2D; |
import java.awt.image.BufferedImage; |
import java.io.IOException; |
import java.io.InputStream; |
import java.io.OutputStream; |
import javax.imageio.ImageIO; |
import jp.sourceforge.qrcode.QRCodeDecoder; |
import jp.sourceforge.qrcode.exception.DecodingFailedException; |
import jp.sourceforge.qrcode.data.QRCodeImage; |
* TwoDimensionCode类:二维码操作核心类 |
public class TwoDimensionCode { |
public void encoderQRCode(String content, String imgPath) { |
this .encoderQRCode(content, imgPath, "png" , 7 ); |
public void encoderQRCode(String content, OutputStream output) { |
this .encoderQRCode(content, output, "png" , 7 ); |
public void encoderQRCode(String content, String imgPath, String imgType) { |
this .encoderQRCode(content, imgPath, imgType, 7 ); |
public void encoderQRCode(String content, OutputStream output, |
this .encoderQRCode(content, output, imgType, 7 ); |
public void encoderQRCode(String content, String imgPath, String imgType, |
BufferedImage bufImg = this .qRCodeCommon(content, imgType, size); |
File imgFile = new File(imgPath); |
ImageIO.write(bufImg, imgType, imgFile); |
public void encoderQRCode(String content, OutputStream output, |
String imgType, int size) { |
BufferedImage bufImg = this .qRCodeCommon(content, imgType, size); |
ImageIO.write(bufImg, imgType, output); |
private BufferedImage qRCodeCommon(String content, String imgType, int size) { |
BufferedImage bufImg = null ; |
Qrcode qrcodeHandler = new Qrcode(); |
qrcodeHandler.setQrcodeErrorCorrect( 'M' ); |
qrcodeHandler.setQrcodeEncodeMode( 'B' ); |
qrcodeHandler.setQrcodeVersion(size); |
byte [] contentBytes = content.getBytes( "utf-8" ); |
int imgSize = 67 + 12 * (size - 1 ); |
bufImg = new BufferedImage(imgSize, imgSize, |
BufferedImage.TYPE_INT_RGB); |
Graphics2D gs = bufImg.createGraphics(); |
gs.setBackground(Color.WHITE); |
gs.clearRect( 0 , 0 , imgSize, imgSize); |
gs.setColor(Color.BLACK); |
if (contentBytes.length > 0 && contentBytes.length < 800 ) { |
boolean [][] codeOut = qrcodeHandler.calQrcode(contentBytes); |
for ( int i = 0 ; i < codeOut.length; i++) { |
for ( int j = 0 ; j < codeOut.length; j++) { |
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3 , 3 ); |
throw new Exception( "QRCode content bytes length = " |
+ contentBytes.length + " not in [0, 800]." ); |
public String decoderQRCode(String imgPath) { |
File imageFile = new File(imgPath); |
BufferedImage bufImg = null ; |
bufImg = ImageIO.read(imageFile); |
QRCodeDecoder decoder = new QRCodeDecoder(); |
content = new String(decoder.decode( new TwoDimensionCodeImage( |
} catch (IOException e) { |
System.out.println( "Error: " + e.getMessage()); |
} catch (DecodingFailedException dfe) { |
System.out.println( "Error: " + dfe.getMessage()); |
public String decoderQRCode(InputStream input) { |
BufferedImage bufImg = null ; |
bufImg = ImageIO.read(input); |
QRCodeDecoder decoder = new QRCodeDecoder(); |
content = new String(decoder.decode( new TwoDimensionCodeImage( |
} catch (IOException e) { |
System.out.println( "Error: " + e.getMessage()); |
} catch (DecodingFailedException dfe) { |
System.out.println( "Error: " + dfe.getMessage()); |
public static void main(String[] args) { |
String imgPath = "C:/temp/temp.png" ; |
TwoDimensionCode handler = new TwoDimensionCode(); |
handler.encoderQRCode(encoderContent, imgPath, "png" ); |
System.out.println( "生成二维码成功" ); |
String decoderContent = handler.decoderQRCode(imgPath); |
System.out.println( "解析结果如下:" ); |
System.out.println(decoderContent); |
System.out.println( "解析二维码成功" ); |
* TwoDimensionCodeImage 类:二维码图片对象 |
class TwoDimensionCodeImage implements QRCodeImage { |
public TwoDimensionCodeImage(BufferedImage bufImg) { |
return bufImg.getHeight(); |
public int getPixel( int x, int y) { |
return bufImg.getRGB(x, y); |
return bufImg.getWidth(); |