java生成彩色附logo二維碼
java生成二維碼有很多開發(fā)的jar包如zxing是谷歌開發(fā)的,這里的話我使用zxing的開發(fā)包來實現(xiàn)的。我們在很多項目中需要動態(tài)生成二維碼,來提供給用戶,這樣讓更多人能夠很好的通過二維碼來體驗自己的應(yīng)用。
下面貼出代碼,已經(jīng)測試通過,大家可以直接復(fù)制代碼使用:
最后實現(xiàn)結(jié)果:
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.io.UnsupportedEncodingException; 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 MatrixToImageWriter { private static final int IMAGE_WIDTH = 100; private static final int IMAGE_HEIGHT = 100; private static final int IMAGE_HALF_WIDTH = IMAGE_WIDTH / 2; private static final int FRAME_WIDTH = 2; private static MultiFormatWriter mutiWriter = new MultiFormatWriter(); 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); // 二維矩陣轉(zhuǎn)為一維像素數(shù)組 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++) { // 左上角顏色,根據(jù)自己需要調(diào)整顏色范圍和顏色 if (x > 0 && x < 170 && y > 0 && y < 170) { Color color = new Color(231, 144, 56); int colorInt = color.getRGB(); pixels[y * width + x] = matrix.get(x, y) ? colorInt : 16777215; } // 讀取圖片 else 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 > halfW - 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 { // 二維碼顏色 int num1 = (int) (50 - (50.0 - 13.0) / matrix.getHeight() * (y + 1)); int num2 = (int) (165 - (165.0 - 72.0) / matrix.getHeight() * (y + 1)); int num3 = (int) (162 - (162.0 - 107.0) / matrix.getHeight() * (y + 1)); Color color = new Color(num1, num2, num3); int colorInt = color.getRGB(); // 此處可以修改二維碼的顏色,可以分別制定二維碼和背景的顏色; pixels[y * width + x] = matrix.get(x, y) ? colorInt : 16777215; // 0x000000:0xffffff } } } BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.getRaster().setDataElements(0, 0, width, height, pixels); return image; } 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) { // 補(bǔ)白 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); else graphic.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) throws UnsupportedEncodingException { // 依次為內(nèi)容(不支持中文),寬,長,中間圖標(biāo)路徑,儲存路徑 MatrixToImageWriter.encode("http://www.baidu.com/", 512, 512, "D:\\logo.png", "D:\\2013-01.jpg"); } }
大家可以參考專題:java二維碼進(jìn)行學(xué)習(xí)
以上就是本文的全部內(nèi)容,幫助大家設(shè)計屬于自己的二維碼。
相關(guān)文章
SpringBoot詳解shiro過濾器與權(quán)限控制
當(dāng)shiro被運(yùn)用到web項目時,shiro會自動創(chuàng)建一些默認(rèn)的過濾器對客戶端請求進(jìn)行過濾。比如身份驗證、授權(quán)的相關(guān)的,這篇文章主要介紹了shiro過濾器與權(quán)限控制2022-07-07最新hadoop安裝教程及hadoop的命令使用(親測可用)
這篇文章主要介紹了最新hadoop安裝教程(親測可用),本文主要講解了如何安裝hadoop、使用hadoop的命令及遇到的問題解決,需要的朋友可以參考下2022-06-06Spring很常用的@Conditional注解的使用場景和源碼解析
今天要分享的是Spring的注解@Conditional,@Conditional是一個條件注解,它的作用是判斷Bean是否滿足條件,本文詳細(xì)介紹了@Conditional注解的使用場景和源碼,需要的朋友可以參考一下2023-04-04解決springboot中配置過濾器以及可能出現(xiàn)的問題
這篇文章主要介紹了解決springboot中配置過濾器以及可能出現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09關(guān)于@JsonProperty和@JSONField注解的區(qū)別及用法
這篇文章主要介紹了關(guān)于@JsonProperty和@JSONField注解的區(qū)別及用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08如何基于Springboot完成新增員工功能并設(shè)置全局異常處理器
最近工作中遇到了做一個管理員工信息的功能,下面這篇文章主要給大家介紹了關(guān)于如何基于Springboot完成新增員工功能并設(shè)置全局異常處理器的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11