日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java实现二维码技术探讨。

發布時間:2024/4/15 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java实现二维码技术探讨。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java生成二維碼方法有三種:

1: 使用SwetakeQRCode在Java項目中生成二維碼?
http://swetake.com/qr/ 下載地址?
或著http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip?
這個是日本人寫的,生成的是我們常見的方形的二維碼?
能夠用中文?如:5677777ghjjjjj?

2: 使用BarCode4j生成條形碼和二維碼?
BarCode4j網址:http://sourceforge.net/projects/barcode4j/?

barcode4j是使用datamatrix的二維碼生成算法,為支持qr的算法?
datamatrix是歐美的標準。qr為日本的標準,?
barcode4j一般生成出來是長方形的

如:88777alec000yan?
這個博客這方面說的挺清楚的:?
http://baijinshan.iteye.com/blog/1004554


3、如今具體介紹第三種也是經常使用的方式:利用Zxing生成二維碼

Zxing是Google提供的關于條碼(一維碼、二維碼)的解析工具,提供了二維碼的生成與解析的方法,如今我簡介一下使用Java利用Zxing生成與解析二維碼。

第一步:將Zxing-core.jar 包增加項目中。

第二步 加入下面三個Java文件,三個文件功能例如以下:

MatrixToImageWriter.java ?生成二維碼圖片。

BufferedImageLuminanceSource.java ? 解析二維碼圖片。

BarcodeFactory.java ?生成帶頭像的二維碼。


MatrixToImageWriter.java

import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix;import javax.imageio.ImageIO; import java.io.File; import java.io.OutputStream; import java.io.IOException; import java.util.Hashtable; import java.awt.image.BufferedImage;public final class MatrixToImageWriter {private static final int BLACK = 0xFF000000;private static final int WHITE = 0xFFFFFFFF;private MatrixToImageWriter() {}public static BufferedImage toBufferedImage(BitMatrix matrix) {int width = matrix.getWidth();int height = matrix.getHeight();BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, matrix.get(x, y) ?

BLACK : WHITE); } } return image; } public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } } public static void main(String[] args) throws Exception { String text = "二維碼內容,能夠是鏈接也能夠是文本"; int width = 300; int height = 300; Hashtable hints = new Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//內容所使用編碼 BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); File outputFile = new File("E:/qrtest/生成普通二維碼.jpg"); //File.separator String format = "jpg";//二維碼的圖片格式 MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); } }


BufferedImageLuminanceSource.java

import com.google.zxing.Binarizer; import com.google.zxing.BinaryBitmap; import com.google.zxing.EncodeHintType; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.Result; import com.google.zxing.common.HybridBinarizer;import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.File; import java.util.HashMap; import java.util.Map;import javax.imageio.ImageIO;public final class BufferedImageLuminanceSource extends LuminanceSource {private final BufferedImage image;private final int left;private final int top;public BufferedImageLuminanceSource(BufferedImage image) {this(image, 0, 0, image.getWidth(), image.getHeight());}public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {super(width, height);int sourceWidth = image.getWidth();int sourceHeight = image.getHeight();if (left + width > sourceWidth || top + height > sourceHeight) {throw new IllegalArgumentException("Crop rectangle does not fit within image data.");}for (int y = top; y < top + height; y++) {for (int x = left; x < left + width; x++) {if ((image.getRGB(x, y) & 0xFF000000) == 0) {image.setRGB(x, y, 0xFFFFFFFF); // = white}}}this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);this.image.getGraphics().drawImage(image, 0, 0, null);this.left = left;this.top = top;}@Overridepublic byte[] getRow(int y, byte[] row) {if (y < 0 || y >= getHeight()) {throw new IllegalArgumentException("Requested row is outside the image: " + y);}int width = getWidth();if (row == null || row.length < width) {row = new byte[width];}image.getRaster().getDataElements(left, top + y, width, 1, row);return row;}@Overridepublic byte[] getMatrix() {int width = getWidth();int height = getHeight();int area = width * height;byte[] matrix = new byte[area];image.getRaster().getDataElements(left, top, width, height, matrix);return matrix;}@Overridepublic boolean isCropSupported() {return true;}@Overridepublic LuminanceSource crop(int left, int top, int width, int height) {return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);}@Overridepublic boolean isRotateSupported() {return true;}@Overridepublic LuminanceSource rotateCounterClockwise() {int sourceWidth = image.getWidth();int sourceHeight = image.getHeight();AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);Graphics2D g = rotatedImage.createGraphics();g.drawImage(image, transform, null);g.dispose();int width = getWidth();return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);}public static void main(String[] args) {//解析二維碼try {MultiFormatReader formatReader = new MultiFormatReader();String filePath = "E:/qrtest/生成普通二維碼.jpg";File file = new File(filePath);BufferedImage image = ImageIO.read(file);LuminanceSource source = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(source);BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);Map hints = new HashMap();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");Result result = formatReader.decode(binaryBitmap, hints);System.out.println("result = " + result.toString());System.out.println("resultFormat = " + result.getBarcodeFormat());System.out.println("resultText = " + result.getText());} catch (Exception e) {e.printStackTrace();}} }
BarcodeFactory.java

import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map;import javax.imageio.ImageIO;import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;public class BarcodeFactory {// 圖片寬度的一般private static final int IMAGE_WIDTH = 80;private static final int IMAGE_HEIGHT = 80;private static final int IMAGE_HALF_WIDTH = IMAGE_WIDTH / 2;private static final int FRAME_WIDTH = 2;// 二維碼寫碼器private static MultiFormatWriter mutiWriter = new MultiFormatWriter();/*** 將srcImagePath嵌入到destImagePath*/public static void encode(String content, int width, int height, String srcImagePath, String destImagePath) {try {ImageIO.write(genBarcode(content, width, height, srcImagePath), "jpg", new File(destImagePath));} catch (IOException e) {e.printStackTrace();} catch (WriterException e) {e.printStackTrace();}}private static BufferedImage genBarcode(String content, int width, int height, String srcImagePath)throws WriterException, IOException {// 讀取源圖像BufferedImage scaleImage = scale(srcImagePath, IMAGE_WIDTH, IMAGE_HEIGHT, true);int[][] srcPixels = new int[IMAGE_WIDTH][IMAGE_HEIGHT];for (int i = 0; i < scaleImage.getWidth(); i++) {for (int j = 0; j < scaleImage.getHeight(); j++) {srcPixels[i][j] = scaleImage.getRGB(i, j);}}Map<EncodeHintType, Object> hint = new HashMap<EncodeHintType, Object>();hint.put(EncodeHintType.CHARACTER_SET, "utf-8");hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 生成二維碼BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hint);// 二維矩陣轉為一維像素數組int halfW = matrix.getWidth() / 2;int halfH = matrix.getHeight() / 2;int[] pixels = new int[width * height];for (int y = 0; y < matrix.getHeight(); y++) {for (int x = 0; x < matrix.getWidth(); x++) {// 讀取圖片if (x > halfW - IMAGE_HALF_WIDTH && x < halfW + IMAGE_HALF_WIDTH && y > halfH - IMAGE_HALF_WIDTH&& y < halfH + IMAGE_HALF_WIDTH) {pixels[y * width + x] = srcPixels[x - halfW + IMAGE_HALF_WIDTH][y - halfH + IMAGE_HALF_WIDTH];}// 在圖片四周形成邊框else if ((x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW - IMAGE_HALF_WIDTH + FRAME_WIDTH&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)|| (x > halfW + IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)|| (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH - IMAGE_HALF_WIDTH + FRAME_WIDTH)|| (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH&& y > halfH + IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)) {pixels[y * width + x] = 0xfffffff;} else {// 此處能夠改動二維碼的顏色,能夠分別制定二維碼和背景的顏色;pixels[y * width + x] = matrix.get(x, y) ? 0xff000000 : 0xfffffff;}}}BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);image.getRaster().setDataElements(0, 0, width, height, pixels);return image;}/*** 把傳入的原始圖像按高度和寬度進行縮放。生成符合要求的圖標* * @param srcImageFile* 源文件地址* @param height* 目標高度* @param width* 目標寬度* @param hasFiller* 比例不正確時是否須要補白:true為補白; false為不補白;* @throws IOException*/private static BufferedImage scale(String srcImageFile, int height, int width, boolean hasFiller) throws IOException {double ratio = 0.0; // 縮放比例File file = new File(srcImageFile);BufferedImage srcImage = ImageIO.read(file);Image destImage = srcImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);// 計算比例if ((srcImage.getHeight() > height) || (srcImage.getWidth() > width)) {if (srcImage.getHeight() > srcImage.getWidth()) {ratio = (new Integer(height)).doubleValue() / srcImage.getHeight();} else {ratio = (new Integer(width)).doubleValue() / srcImage.getWidth();}AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);destImage = op.filter(srcImage, null);}if (hasFiller) {// 補白BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D graphic = image.createGraphics();graphic.setColor(Color.white);graphic.fillRect(0, 0, width, height);if (width == destImage.getWidth(null))graphic.drawImage(destImage, 0, (height - destImage.getHeight(null)) / 2, destImage.getWidth(null), destImage.getHeight(null), Color.white, null);elsegraphic.drawImage(destImage, (width - destImage.getWidth(null)) / 2, 0, destImage.getWidth(null), destImage.getHeight(null), Color.white, null);graphic.dispose();destImage = image;}return (BufferedImage) destImage;}public static void main(String[] args) {//src.jpg為頭像文件BarcodeFactory.encode("http://www.baidu.com", 300, 300, "E:/qrtest/src.jpg","E:/qrtest/生成帶頭像的二維碼.jpg");} }


轉載于:https://www.cnblogs.com/gccbuaa/p/6740964.html

總結

以上是生活随笔為你收集整理的Java实现二维码技术探讨。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。