提及二維碼,微信好像最早啓用,隨後各種二維碼就開始流行起來了。那什麼是二維碼呢。java
1.什麼是二維碼?百度一下便可
微信
http://baike.baidu.com/view/132241.htm?fr=aladdin框架
2.java開發二維碼?this
2.1:首先導入:此包google
下載地址:http://pan.baidu.com/s/1jGJwPwU
spa
2.2:java類code
1 import java.awt.image.BufferedImage; 2 3 /** 4 * Encapsulates custom configuration used in methods of {@link MatrixToImageWriter}. 5 */ 6 public final class MatrixToImageConfig { 7 8 public static final int BLACK = 0xFF000000; 9 public static final int WHITE = 0xFFFFFFFF; 10 11 private final int onColor; 12 private final int offColor; 13 14 /** 15 * Creates a default config with on color {@link #BLACK} and off color {@link #WHITE}, generating normal 16 * black-on-white barcodes. 17 */ 18 public MatrixToImageConfig() { 19 this(BLACK, WHITE); 20 } 21 22 /** 23 * @param onColor pixel on color, specified as an ARGB value as an int 24 * @param offColor pixel off color, specified as an ARGB value as an int 25 */ 26 public MatrixToImageConfig(int onColor, int offColor) { 27 this.onColor = onColor; 28 this.offColor = offColor; 29 } 30 31 public int getPixelOnColor() { 32 return onColor; 33 } 34 35 public int getPixelOffColor() { 36 return offColor; 37 } 38 39 int getBufferedImageColorModel() { 40 // Use faster BINARY if colors match default 41 return onColor == BLACK && offColor == WHITE ? BufferedImage.TYPE_BYTE_BINARY : BufferedImage.TYPE_INT_RGB; 42 } 43 44 }
1 import java.awt.image.BufferedImage; 2 import java.io.File; 3 import java.io.IOException; 4 import java.io.OutputStream; 5 6 import javax.imageio.ImageIO; 7 8 import com.google.zxing.common.BitMatrix; 9 public class MatrixToImageWriter { 10 private static final int BLACK = 0xFF000000; 11 private static final int WHITE = 0xFFFFFFFF; 12 13 private MatrixToImageWriter() {} 14 15 16 public static BufferedImage toBufferedImage(BitMatrix matrix) { 17 int width = matrix.getWidth(); 18 int height = matrix.getHeight(); 19 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 20 for (int x = 0; x < width; x++) { 21 for (int y = 0; y < height; y++) { 22 image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); 23 } 24 } 25 return image; 26 } 27 28 29 public static void writeToFile(BitMatrix matrix, String format, File file) 30 throws IOException { 31 BufferedImage image = toBufferedImage(matrix); 32 if (!ImageIO.write(image, format, file)) { 33 throw new IOException("Could not write an image of format " + format + " to " + file); 34 } 35 } 36 37 38 public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) 39 throws IOException { 40 BufferedImage image = toBufferedImage(matrix); 41 if (!ImageIO.write(image, format, stream)) { 42 throw new IOException("Could not write an image of format " + format); 43 } 44 } 45 }
test類:orm
1 import java.io.File; 2 import java.util.Hashtable; 3 4 import com.google.zxing.BarcodeFormat; 5 import com.google.zxing.EncodeHintType; 6 import com.google.zxing.MultiFormatWriter; 7 import com.google.zxing.common.BitMatrix; 8 9 public class Test { 10 11 public static void main(String[] args) throws Exception{ 12 String text = "http://www.baidu.com";//二維碼的內容 13 int width = 400; 14 int height = 400; 15 String format = "png"; 16 Hashtable hints= new Hashtable(); 17 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 18 BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints); 19 File outputFile = new File("E:/codeImg"); 20 MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); 21 System.out.println("It is ok!"); 22 23 } 24 }
以上用的是一個谷歌提供的開源框架首先的,呵呵。。。站在巨人的肩膀上使用....htm
效果圖:blog