Java二維碼的解碼和編碼

來源:互聯網
上載者:User

標籤:min   buffer   import   bsp   text   htable   map   nan   format   

原文:http://www.open-open.com/code/view/1430906793866

 

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;  //編碼public class QREncoder {      public static void main(String[] args) throws Exception {        String content = "hello world";        String encoding = "utf-8";        int width = 150;        int height = 150;        String format = "jpg";        File imagePath = new File("d:/test.jpg");                  com.google.zxing.Writer writer = new MultiFormatWriter();        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);        hints.put(EncodeHintType.CHARACTER_SET, encoding);        BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);        MatrixToImageWriter.writeToFile(matrix, format, imagePath);    }  }      import java.io.File;import java.util.Hashtable;  import javax.imageio.ImageIO;  import com.google.zxing.Binarizer;import com.google.zxing.BinaryBitmap;import com.google.zxing.DecodeHintType;import com.google.zxing.LuminanceSource;import com.google.zxing.MultiFormatReader;import com.google.zxing.Reader;import com.google.zxing.Result;import com.google.zxing.client.j2se.BufferedImageLuminanceSource;import com.google.zxing.common.GlobalHistogramBinarizer;  //解碼public class QRDecoder {      public static void main(String[] args) throws Exception {        File imagePath = new File("d:/test.jpg");        String encoding = "utf-8";                  Reader reader = new MultiFormatReader();        LuminanceSource ls = new BufferedImageLuminanceSource(ImageIO.read(imagePath));        Binarizer b = new GlobalHistogramBinarizer(ls);        Hashtable<DecodeHintType, String> hint1 = new Hashtable<DecodeHintType, String>(2);        hint1.put(DecodeHintType.CHARACTER_SET, encoding);        Result r = reader.decode(new BinaryBitmap(b), hint1);        System.out.println(r.getText());    }  }

 

Java二維碼的解碼和編碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.