JAVA实现二维码、条形码生成与破译-开源Zxing使用研究
二维码编码代码实现类:
package zxing;
import java.io.File; ? ??
import java.util.Hashtable; ? ??
? ??
import com.google.zxing.BarcodeFormat; ? ??
import com.google.zxing.EncodeHintType; ? ??
import com.google.zxing.MultiFormatWriter; ? ??
import com.google.zxing.client.j2se.MatrixToImageWriter; ? ??
import com.google.zxing.common.BitMatrix; ? ??
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; ? ??
? ??
/** ??
?* @blog http://gaozzsoft.iteye.com ??
?* @author gaozzsoft ??
?*/ ? ?
public class ZxingEncoderHandler { ? ??
? ??
? ? /** ??
? ? ?* 编码 ??
? ? ?* @param contents ??
? ? ?* @param width ??
? ? ?* @param height ??
? ? ?* @param imgPath ??
? ? ?*/ ? ?
? ? public void encode(String contents, int width, int height, String imgPath) { ? ??
? ? ? ? Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); ? ??
? ? ? ? // 指定纠错等级 ? ??
? ? ? ? hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); ? ??
? ? ? ? // 指定编码格式 ? ??
? ? ? ? hints.put(EncodeHintType.CHARACTER_SET, "GBK"); ? ??
? ? ? ? try { ? ??
? ? ? ? ? ? BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, ? ??
? ? ? ? ? ? ? ? ? ? BarcodeFormat.QR_CODE, width, height, hints); ? ??
? ??
? ? ? ? ? ? MatrixToImageWriter ? ??
? ? ? ? ? ? ? ? ? ? .writeToFile(bitMatrix, "png", new File(imgPath)); ? ??
? ??
? ? ? ? } catch (Exception e) { ? ??
? ? ? ? ? ? e.printStackTrace(); ? ??
? ? ? ? } ? ??
? ? } ? ??
? ??
? ? /** ??
? ? ?* @param args ??
? ? ?*/ ? ?
? ? public static void main(String[] args) { ? ??
? ? ? ? String imgPath = "e:/michael_zxing.png"; ? ??
? ? ? ? String contents = "Hello Michael(大大),welcome to Zxing!" ? ?
? ? ? ? ? ? ? ? + "\nMichael’s blog [ http://sjsky.iteye.com ]" ? ?
? ? ? ? ? ? ? ? + "\nEMail [ sjsky007@gmail.com ]" + "\nTwitter [ @suncto ]"; ? ??
? ? ? ? int width = 300, height = 300; ? ??
? ? ? ? ZxingEncoderHandler handler = new ZxingEncoderHandler(); ? ??
? ? ? ? handler.encode(contents, width, height, imgPath); ? ??
? ??
? ? ? ? System.out.println("Michael ,you have finished zxing encode."); ? ??
? ? } ? ??
} ? ?
二维码解码代码实现类:
package zxing;
import java.awt.image.BufferedImage; ? ??
import java.io.File; ? ??
import java.util.Hashtable; ? ??
? ??
import javax.imageio.ImageIO; ? ??
? ??
import com.google.zxing.BinaryBitmap; ? ??
import com.google.zxing.DecodeHintType; ? ??
import com.google.zxing.LuminanceSource; ? ??
import com.google.zxing.MultiFormatReader; ? ??
import com.google.zxing.Result; ? ??
import com.google.zxing.client.j2se.BufferedImageLuminanceSource; ? ??
import com.google.zxing.common.HybridBinarizer; ? ??
? ??
/** ??
?* @blog http://gaozzsoft.iteye.com ??
?* @author gaozzsoft ??
?*/ ? ?
public class ZxingDecoderHandler { ? ??
? ??
? ? /** ??
? ? ?* @param imgPath ??
? ? ?* @return String ??
? ? ?*/ ? ?
? ? public String decode(String imgPath) { ? ??
? ? ? ? BufferedImage image = null; ? ??
? ? ? ? Result result = null; ? ??
? ? ? ? try { ? ??
? ? ? ? ? ? image = ImageIO.read(new File(imgPath)); ? ??
? ? ? ? ? ? if (image == null) { ? ??
? ? ? ? ? ? ? ? System.out.println("the decode image may be not exit."); ? ??
? ? ? ? ? ? } ? ??
? ? ? ? ? ? LuminanceSource source = new BufferedImageLuminanceSource(image); ? ??
? ? ? ? ? ? BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); ? ??
? ??
? ? ? ? ? ? Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); ? ??
? ? ? ? ? ? hints.put(DecodeHintType.CHARACTER_SET, "GBK"); ? ??
? ? ? ? ? ? result = new MultiFormatReader().decode(bitmap, hints);
? ? ? ? ? ??
? ? ? ? ? ? return result.getText(); ? ??
? ? ? ? } catch (Exception e) { ? ??
? ? ? ? ? ? e.printStackTrace(); ? ??
? ? ? ? } ? ??
? ? ? ? return null; ? ??
? ? } ? ??
? ??
? ? /** ??
? ? ?* @param args ??
? ? ?*/ ? ?
? ? public static void main(String[] args) { ? ??
? ? ? ? String imgPath = "e:/michael_zxing.png"; ? ??
? ? ? ? ZxingDecoderHandler handler = new ZxingDecoderHandler(); ? ??
? ? ? ? String decodeContent = handler.decode(imgPath); ? ??
? ? ? ? System.out.println("解码内容如下:"); ? ??
? ? ? ? System.out.println(decodeContent); ? ??
? ? ? ? System.out.println("Michael ,you have finished zxing decode."); ? ??
? ??
? ? } ? ??
} ? ?
条形码编码代码实现类:
package zxing;
import java.io.File; ? ??
import com.google.zxing.BarcodeFormat; ? ??
import com.google.zxing.MultiFormatWriter; ? ??
import com.google.zxing.client.j2se.MatrixToImageWriter; ? ??
import com.google.zxing.common.BitMatrix; ? ??
? ??
/** ??
?* @blog http://gaozzsoft.iteye.com ??
?* @author gaozzsoft ??
?*/?
public class ZxingEAN13EncoderHandler { ? ??
? ??
? ? /** ??
? ? ?* 编码 ??
? ? ?* @param contents ??
? ? ?* @param width ??
? ? ?* @param height ??
? ? ?* @param imgPath ??
? ? ?*/ ? ?
? ? public void encode(String contents, int width, int height, String imgPath) { ? ??
? ? ? ? int codeWidth = 3 + // start guard ? ??
? ? ? ? ? ? ? ? (7 * 6) + // left bars ? ??
? ? ? ? ? ? ? ? 5 + // middle guard ? ??
? ? ? ? ? ? ? ? (7 * 6) + // right bars ? ??
? ? ? ? ? ? ? ? 3; // end guard ? ??
? ? ? ? codeWidth = Math.max(codeWidth, width); ? ??
? ? ? ? try { ? ??
? ? ? ? ? ? BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, ? ??
? ? ? ? ? ? ? ? ? ? BarcodeFormat.EAN_13, codeWidth, height, null); ? ??
? ??
? ? ? ? ? ? MatrixToImageWriter ? ??
? ? ? ? ? ? ? ? ? ? .writeToFile(bitMatrix, "png", new File(imgPath)); ? ??
? ??
? ? ? ? } catch (Exception e) { ? ??
? ? ? ? ? ? e.printStackTrace(); ? ??
? ? ? ? } ? ??
? ? } ? ??
? ??
? ? /** ??
? ? ?* @param args ??
? ? ?*/ ? ?
? ? public static void main(String[] args) { ? ??
? ? ? ? String imgPath = "e:/zxing_EAN13.png"; ? ??
? ? ? ? // 益达无糖口香糖的条形码 ? ??
? ? ? ? String contents = "6923450657713"; ? ??
? ??
? ? ? ? int width = 105, height = 50; ? ??
? ? ? ? ZxingEAN13EncoderHandler handler = new ZxingEAN13EncoderHandler(); ? ??
? ? ? ? handler.encode(contents, width, height, imgPath); ? ??
? ??
? ? ? ? System.out.println("Michael ,you have finished zxing EAN13 encode."); ? ??
? ? } ? ??
} ? ?
条形码解码代码实现类:
package zxing;
import java.awt.image.BufferedImage; ? ??
import java.io.File; ? ??
? ??
import javax.imageio.ImageIO; ? ??
? ??
import com.google.zxing.BinaryBitmap; ? ??
import com.google.zxing.LuminanceSource; ? ??
import com.google.zxing.MultiFormatReader; ? ??
import com.google.zxing.Result; ? ??
import com.google.zxing.client.j2se.BufferedImageLuminanceSource; ? ??
import com.google.zxing.common.HybridBinarizer; ? ??
? ??
/** ??
?* @blog http://gaozzsoft.iteye.com ??
?* @author gaozzsoft ??
?*/ ? ??
public class ZxingEAN13DecoderHandler { ? ??
? ??
? ? /** ??
? ? ?* @param imgPath ??
? ? ?* @return String ??
? ? ?*/ ? ?
? ? public String decode(String imgPath) { ? ??
? ? ? ? BufferedImage image = null; ? ??
? ? ? ? Result result = null; ? ??
? ? ? ? try { ? ??
? ? ? ? ? ? image = ImageIO.read(new File(imgPath)); ? ??
? ? ? ? ? ? if (image == null) { ? ??
? ? ? ? ? ? ? ? System.out.println("the decode image may be not exit."); ? ??
? ? ? ? ? ? } ? ??
? ? ? ? ? ? LuminanceSource source = new BufferedImageLuminanceSource(image); ? ??
? ? ? ? ? ? BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); ? ??
? ??
? ? ? ? ? ? result = new MultiFormatReader().decode(bitmap, null); ? ??
? ? ? ? ? ? return result.getText(); ? ??
? ? ? ? } catch (Exception e) { ? ??
? ? ? ? ? ? e.printStackTrace(); ? ??
? ? ? ? } ? ??
? ? ? ? return null; ? ??
? ? } ? ??
? ??
? ? /** ??
? ? ?* @param args ??
? ? ?*/ ? ?
? ? public static void main(String[] args) { ? ??
? ? ? ? String imgPath = "e:/zxing_EAN13.png"; ? ??
? ? ? ? ZxingEAN13DecoderHandler handler = new ZxingEAN13DecoderHandler(); ? ??
? ? ? ? String decodeContent = handler.decode(imgPath); ? ??
? ? ? ? System.out.println("解码内容如下:"); ? ??
? ? ? ? System.out.println(decodeContent); ? ??
? ? ? ? System.out.println("Michael ,you have finished zxing EAN-13 decode."); ? ??
? ??
? ? } ? ??
} ?
?
Import Jars:
core.jar、javase.jar(需要下载zxing利用里边的这两个JAR足够了)