Package com.sm.framework.util.qrcode

Source Code of com.sm.framework.util.qrcode.Sample

package com.sm.framework.util.qrcode;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;



public class Sample {

  /**
   * ������
   *
   * @param
   * @param
   * @return void
   * ����12:54:59 ��С��
   **/
  public static void main(String[] args) {
    String imgPath = "F:/TDDOWNLOAD/Michael_QRCode.png"
    String encoderContent = "������"+"\n"+"����Ů"+"\n"+"�����ж�����";
    Sample handler = new Sample();
    handler.encoderQRCode(encoderContent, imgPath);
    System.out.println("ok");

  }
  private BufferedImage qRCodeCommon(String content, String imgType, int size) { 
            BufferedImage bufImg = null
            try
                Qrcode qrcodeHandler = new Qrcode()
               // ���ö�ά���Ŵ��ʣ���ѡL(7%)��M(15%)��Q(25%)��H(30%)���Ŵ���Խ�߿ɴ洢����ϢԽ�٣����Զ�ά�������ȵ�Ҫ��ԽС 
                qrcodeHandler.setQrcodeErrorCorrect('M')
                qrcodeHandler.setQrcodeEncodeMode('B')
                // �������ö�ά��ߴ磬ȡֵ��Χ1-40��ֵԽ��ߴ�Խ�󣬿ɴ洢����ϢԽ�� 
                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)
   
               // �趨ͼ����ɫ> BLACK 
               gs.setColor(Color.BLACK)
                // ����ƫ�����������ÿ��ܵ��½������� 
               int pixoff = 2
                // �������> ��ά�� 
               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++) { 
                           if (codeOut[j][i]) { 
                           gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3)
                           
                        } 
                    } 
              } else
                  throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].")
               
              gs.dispose()
              bufImg.flush()
           } catch (Exception e) { 
             e.printStackTrace()
           
            return bufImg; 
       

  public void encoderQRCode(String content, String imgPath, String imgType, int size) { 
            try
               BufferedImage bufImg = this.qRCodeCommon(content, imgType, size)
                
            File imgFile = new File(imgPath)
               // ���ɶ�ά��QRCodeͼƬ 
                ImageIO.write(bufImg, imgType, imgFile)
          } catch (Exception e) { 
               e.printStackTrace()
          
        }

   public void encoderQRCode(String content, String imgPath) { 
             this.encoderQRCode(content, imgPath, "png", 7)
        

 
}
TOP

Related Classes of com.sm.framework.util.qrcode.Sample

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.