
1 package imooc.zxing; 2 3 import java.io.File; 4 import java.nio.file.Path; 5 import java.util.HashMap; 6 7 import com.google.zxing.BarcodeFormat; 8 import com.google.zxing.EncodeHintType; 9 import com.google.zxing.MultiFormatWriter; 10 import com.google.zxing.WriterException; 11 import com.google.zxing.client.j2se.MatrixToImageWriter; 12 import com.google.zxing.common.BitMatrix; 13 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 14 15 /** 16 * zxing生成二维码 17 * @author 18430 18 * 19 */ 20 public class Zxing { 21 public static void main(String[] args) { 22 int width=300; 23 int height=300; 24 String format="png"; 25 String content="www.imooc.com"; 26 27 HashMap hints=new HashMap(); 28 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 29 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); 30 hints.put(EncodeHintType.MARGIN, 2); 31 32 try { 33 BitMatrix BitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints); 34 Path file=new File("C:/Users/18430/OneDrive/Documents/img.png").toPath(); 35 MatrixToImageWriter.writeToPath(BitMatrix, format, file); 36 } catch (Exception e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 41 } 42 43 }

1 package imooc.zxing; 2 3 import java.awt.image.BufferedImage; 4 import java.io.File; 5 import java.io.IOException; 6 import java.util.HashMap; 7 8 import javax.imageio.ImageIO; 9 10 import com.google.zxing.BinaryBitmap; 11 import com.google.zxing.EncodeHintType; 12 import com.google.zxing.MultiFormatReader; 13 import com.google.zxing.NotFoundException; 14 import com.google.zxing.Result; 15 import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 16 import com.google.zxing.common.HybridBinarizer; 17 18 /** 19 * 解析二维码 20 * 21 * @author 18430 22 * 23 */ 24 25 public class Readqrcode { 26 27 public static void main(String[] args) { 28 try { 29 MultiFormatReader formatReader = new MultiFormatReader(); 30 File file = new File("C:/Users/18430/OneDrive/Documents/img.png"); 31 32 BufferedImage image; 33 34 image = ImageIO.read(file); 35 BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); 36 37 HashMap hints = new HashMap(); 38 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 39 40 Result result = formatReader.decode(binaryBitmap); 41 System.out.println("解析结果:"+result.toString()); 42 System.out.println("二维码格式:"+result.getBarcodeFormat()); 43 System.out.println("二维码文本内容:"+result.getText()); 44 } catch (IOException e1) { 45 // TODO Auto-generated catch block 46 e1.printStackTrace(); 47 } 48 49 catch (NotFoundException e) { 50 // TODO Auto-generated catch block 51 e.printStackTrace(); 52 } 53 54 } 55 }

1 package imooc.qrcoe; 2 3 import java.awt.Color; 4 import java.awt.Graphics2D; 5 import java.awt.image.BufferedImage; 6 import java.io.File; 7 import java.io.IOException; 8 import java.io.UnsupportedEncodingException; 9 10 import javax.imageio.ImageIO; 11 12 import org.ietf.jgss.GSSContext; 13 14 import com.swetake.util.Qrcode; 15 16 /** 17 * qrcode生成二维码 18 * @author 18430 19 * 20 */ 21 public class CreateQrcode { 22 23 public static void main(String[] args) throws UnsupportedEncodingException { 24 Qrcode x=new Qrcode(); 25 x.setQrcodeErrorCorrect('M');//纠错等级 26 x.setQrcodeEncodeMode('B');//‘N’代表数字 'A'代表a-Z 'B'代表其他字符 27 x.setQrcodeVersion(7);//版本 28 String qrData="www.imooc.com"; 29 int height=67+12*(7-1);//67+12*(版本号-1) 30 int width=67+12*(7-1); 31 32 BufferedImage bufferedImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 33 Graphics2D gs=bufferedImage.createGraphics(); 34 gs.setBackground(Color.white); 35 gs.setColor(Color.black); 36 gs.clearRect(0, 0, width, height); 37 38 int pixoff=2;//偏移量(避免解析出错) 39 //往画板里填充内容 40 byte[] d=qrData.getBytes("gb2312"); 41 if(d.length>0 && d.length<120){ 42 boolean[][] s=x.calQrcode(d); 43 44 for(int i=0;i<s.length;i++){ 45 for(int j=0;j<s.length;j++){ 46 if(s[j][i]){ 47 gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3); 48 } 49 } 50 51 } 52 } 53 gs.dispose(); 54 bufferedImage.flush(); 55 56 try { 57 ImageIO.write(bufferedImage, "png", new File("C:/Users/18430/OneDrive/Documents/img.png")); 58 } catch (IOException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } 62 } 63 64 }