<blockquote> <p>本文中实现二维码的操作依赖于google的zxing,需要添加的jar包有:core.jar 和 javase.jar</p> <p>依赖的类有:</p> <div> <pre><span style="color: #0000ff">import</span> javax.imageio.ImageIO;
<span style="color: #0000ff">import</span> com.google.zxing.BarcodeFormat; <span style="color: #0000ff">import</span> com.google.zxing.BinaryBitmap; <span style="color: #0000ff">import</span> com.google.zxing.DecodeHintType; <span style="color: #0000ff">import</span> com.google.zxing.EncodeHintType; <span style="color: #0000ff">import</span> com.google.zxing.LuminanceSource; <span style="color: #0000ff">import</span> com.google.zxing.MultiFormatReader; <span style="color: #0000ff">import</span> com.google.zxing.MultiFormatWriter; <span style="color: #0000ff">import</span> com.google.zxing.ReaderException; <span style="color: #0000ff">import</span> com.google.zxing.Result; <span style="color: #0000ff">import</span> com.google.zxing.client.j2se.BufferedImageLuminanceSource; <span style="color: #0000ff">import</span> com.google.zxing.client.j2se.MatrixToImageWriter; <span style="color: #0000ff">import</span> com.google.zxing.common.BitMatrix; <span style="color: #0000ff">import</span> com.google.zxing.common.HybridBinarizer;</pre>
</div>
<br />
<h4>二维码编码:</h4>
<div> <pre><span style="color: #008000">/** * 二维码编码 * @param str 需要编码的字符串 * @return */</span> @SuppressWarnings("<span style="color: #8b0000">unchecked</span>") <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> File encode(String str) { <span style="color: #0000ff">try</span> { SimpleDateFormat sdDateFormat = <span style="color: #0000ff">new</span> SimpleDateFormat("<span style="color: #8b0000">yyyyMMddhhmmss</span>"); String path = "<span style="color: #8b0000">./encode</span>" + sdDateFormat.format(<span style="color: #0000ff">new</span> Date()) + "<span style="color: #8b0000">.png</span>"; <span style="color: #0000ff">int</span> width = 180; <span style="color: #0000ff">int</span> height = 160; Hashtable hints = <span style="color: #0000ff">new</span> Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, "<span style="color: #8b0000">utf-8</span>"); BitMatrix byteMatrix; byteMatrix = <span style="color: #0000ff">new</span> MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, width, height,hints); File file = <span style="color: #0000ff">new</span> File(path); MatrixToImageWriter.writeToFile(byteMatrix, "<span style="color: #8b0000">png</span>", file); <span style="color: #0000ff">return</span> file; } <span style="color: #0000ff">catch</span> (Exception e) { e.printStackTrace(); <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>; } }</pre> </div>
<br />
<h4>二维码解码:</h4>
<div> <pre><span style="color: #008000">/** * 二维码解码 * @param file 需要解码的文件(如图片) * @return */</span> @SuppressWarnings("<span style="color: #8b0000">unchecked</span>") <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> Result decode(File file) { <span style="color: #0000ff">try</span> { BufferedImage image; <span style="color: #0000ff">try</span> { image = ImageIO.read(file); <span style="color: #0000ff">if</span> (image == <span style="color: #0000ff">null</span>) { System.out.println("<span style="color: #8b0000">Could not decode image</span>"); } LuminanceSource source = <span style="color: #0000ff">new</span> BufferedImageLuminanceSource(image); BinaryBitmap bitmap = <span style="color: #0000ff">new</span> BinaryBitmap(<span style="color: #0000ff">new</span> HybridBinarizer( source)); Result result; Hashtable hints = <span style="color: #0000ff">new</span> Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, "<span style="color: #8b0000">utf-8</span>"); result = <span style="color: #0000ff">new</span> MultiFormatReader().decode(bitmap, hints); <span style="color: #0000ff">return</span> result; } <span style="color: #0000ff">catch</span> (IOException ioe) { System.out.println(ioe.toString()); <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>; } <span style="color: #0000ff">catch</span> (ReaderException re) { System.out.println(re.toString()); <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>; } } <span style="color: #0000ff">catch</span> (Exception ex) { <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>; } }</pre> </div></blockquote>