首页 > 代码库 > java实现二维码
java实现二维码
说起二维码,微信好像最先启用,随后各类二维码就开始流行起来了。那什么是二维码呢。
1.什么是二维码?百度一下即可
http://baike.baidu.com/view/132241.htm?fr=aladdin
2.java开发二维码?
2.1:首先导入:此包
下载地址:http://pan.baidu.com/s/1jGJwPwU
2.2:java类
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 normal16 * 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 int24 * @param offColor pixel off color, specified as an ARGB value as an int25 */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 default41 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类:
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 }
以上用的是一个谷歌提供的开源框架首先的,呵呵。。。站在巨人的肩膀上使用....
效果图:
java实现二维码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。