欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實現(xiàn)識別二維碼圖片功能

 更新時間:2022年04月21日 15:43:17   作者:weijx_  
這篇文章主要為大家詳細介紹了java實現(xiàn)識別二維碼圖片功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java實現(xiàn)識別二維碼圖片功能,供大家參考,具體內(nèi)容如下

所需maven依賴

<dependency>
? ?<groupId>com.google.zxing</groupId>
? ?<artifactId>javase</artifactId>
? ?<version>3.2.1</version>
</dependency>
<dependency>
? ? <groupId>com.google.zxing</groupId>
? ? <artifactId>core</artifactId>
? ? <version>3.3.3</version>
</dependency>

實現(xiàn)的java類

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import sun.misc.BASE64Decoder;
?
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
?* 作用:二維碼識別(圖片)
?* 類名:QRCodeUtils
?**/
public class QRCodeUtils {
? ? /**
? ? ?* 解析二維碼,此方法解析一個路徑的二維碼圖片
? ? ?* path:圖片路徑
? ? ?*/
? ? public static String deEncodeByPath(String path) {
? ? ? ? String content = null;
? ? ? ? BufferedImage image;
? ? ? ? try {
? ? ? ? ? ? image = ImageIO.read(new File(path));
? ? ? ? ? ? LuminanceSource source = new BufferedImageLuminanceSource(image);
? ? ? ? ? ? Binarizer binarizer = new HybridBinarizer(source);
? ? ? ? ? ? BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
? ? ? ? ? ? Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
? ? ? ? ? ? hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
? ? ? ? ? ? Result result = new MultiFormatReader().decode(binaryBitmap, hints);//解碼
? ? ? ? ? ? System.out.println("圖片中內(nèi)容: ?");
? ? ? ? ? ? System.out.println("content: " + result.getText());
? ? ? ? ? ? content = result.getText();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch (NotFoundException e) {
?? ??? ??? ?//這里判斷如果識別不了帶LOGO的圖片,重新添加上一個屬性
? ? ? ? ? ? try {
?? ??? ??? ??? ?image = ImageIO.read(new File(path));
?? ??? ??? ??? ?LuminanceSource source = new BufferedImageLuminanceSource(image);
?? ??? ??? ??? ?Binarizer binarizer = new HybridBinarizer(source);
?? ??? ??? ??? ?BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
?? ??? ??? ??? ?Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
?? ??? ??? ??? ?//設置編碼格式
?? ??? ??? ??? ?hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
?? ??? ??? ??? ?//設置優(yōu)化精度
?? ??? ??? ??? ?hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
?? ??? ??? ??? ?//設置復雜模式開啟(我使用這種方式就可以識別微信的二維碼了)
?? ??? ??? ??? ?hints.put(DecodeHintType.PURE_BARCODE,Boolean.TYPE);
?? ??? ??? ??? ?Result result = new MultiFormatReader().decode(binaryBitmap, hints);//解碼
?? ??? ??? ??? ?System.out.println("圖片中內(nèi)容: ?");
?? ??? ??? ??? ?System.out.println("content: " + result.getText());
?? ??? ??? ??? ?content = result.getText();
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?} catch (NotFoundException e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
? ? ? ? }
? ? ? ? return content;
? ? }
}

測試

public static void main(String [] args){
?? ?deEncodeByPath("D:\\Users/admin/Desktop/erweima/timg (5).jpg");//二維碼圖片路徑
}

輸出結果:

圖片中內(nèi)容:
content: http://qrcode.online

如果上述不能識別的話,那么就需要對圖片處理一次,然后再進行識別,這里是個調(diào)優(yōu)圖片的工具類。

package com.face.ele.common.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
?* @author weijianxing
?* @description: TODO
?* @date 2020/11/26 9:28
?*/
public class ImageOptimizationUtil {

? ? // 閾值0-255
? ? public static int YZ = 150;

? ? /**
? ? ?* 圖像二值化處理
? ? ?*
? ? ?* @param filePath 要處理的圖片路徑
? ? ?* @param fileOutputPath 處理后的圖片輸出路徑
? ? ?*/
? ? public static void binarization(String filePath, String fileOutputPath) throws IOException {
? ? ? ? File file = new File(filePath);
? ? ? ? BufferedImage bi = ImageIO.read(file);
? ? ? ? // 獲取當前圖片的高,寬,ARGB
? ? ? ? int h = bi.getHeight();
? ? ? ? int w = bi.getWidth();
? ? ? ? int arr[][] = new int[w][h];

? ? ? ? // 獲取圖片每一像素點的灰度值
? ? ? ? for (int i = 0; i < w; i++) {
? ? ? ? ? ? for (int j = 0; j < h; j++) {
? ? ? ? ? ? ? ? // getRGB()返回默認的RGB顏色模型(十進制)
? ? ? ? ? ? ? ? arr[i][j] = getImageGray(bi.getRGB(i, j));// 該點的灰度值
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? // 構造一個類型為預定義圖像類型,BufferedImage
? ? ? ? BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);

? ? ? ? // 和預先設置的閾值大小進行比較,大的就顯示為255即白色,小的就顯示為0即黑色
? ? ? ? for (int i = 0; i < w; i++) {
? ? ? ? ? ? for (int j = 0; j < h; j++) {
? ? ? ? ? ? ? ? if (getGray(arr, i, j, w, h) > YZ) {
? ? ? ? ? ? ? ? ? ? int white = new Color(255, 255, 255).getRGB();
? ? ? ? ? ? ? ? ? ? bufferedImage.setRGB(i, j, white);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? int black = new Color(0, 0, 0).getRGB();
? ? ? ? ? ? ? ? ? ? bufferedImage.setRGB(i, j, black);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }

? ? ? ? }
? ? ? ? ImageIO.write(bufferedImage, "jpg", new File(fileOutputPath));
? ? }

? ? /**
? ? ?* 圖像的灰度處理
? ? ?* 利用浮點算法:Gray = R*0.3 + G*0.59 + B*0.11;
? ? ?*
? ? ?* @param rgb 該點的RGB值
? ? ?* @return 返回處理后的灰度值
? ? ?*/
? ? private static int getImageGray(int rgb) {
? ? ? ? String argb = Integer.toHexString(rgb);// 將十進制的顏色值轉為十六進制
? ? ? ? // argb分別代表透明,紅,綠,藍 分別占16進制2位
? ? ? ? int r = Integer.parseInt(argb.substring(2, 4), 16);// 后面參數(shù)為使用進制
? ? ? ? int g = Integer.parseInt(argb.substring(4, 6), 16);
? ? ? ? int b = Integer.parseInt(argb.substring(6, 8), 16);
? ? ? ? int gray = (int) (r*0.28 + g*0.95 + b*0.11);
? ? ? ? return gray;
? ? }

? ? /**
? ? ?* 自己加周圍8個灰度值再除以9,算出其相對灰度值
? ? ?*
? ? ?* @param gray
? ? ?* @param x 要計算灰度的點的橫坐標
? ? ?* @param y 要計算灰度的點的縱坐標
? ? ?* @param w 圖像的寬度
? ? ?* @param h 圖像的高度
? ? ?* @return
? ? ?*/
? ? public static int getGray(int gray[][], int x, int y, int w, int h) {
? ? ? ? int rs = gray[x][y] + (x == 0 ? 255 : gray[x - 1][y]) + (x == 0 || y == 0 ? 255 : gray[x - 1][y - 1])
? ? ? ? ? ? ? ? + (x == 0 || y == h - 1 ? 255 : gray[x - 1][y + 1]) + (y == 0 ? 255 : gray[x][y - 1])
? ? ? ? ? ? ? ? + (y == h - 1 ? 255 : gray[x][y + 1]) + (x == w - 1 ? 255 : gray[x + 1][y])
? ? ? ? ? ? ? ? + (x == w - 1 || y == 0 ? 255 : gray[x + 1][y - 1])
? ? ? ? ? ? ? ? + (x == w - 1 || y == h - 1 ? 255 : gray[x + 1][y + 1]);
? ? ? ? return rs / 9;
? ? }

? ? /**
? ? ?* 二值化后的圖像的開運算:先腐蝕再膨脹(用于去除圖像的小黑點)
? ? ?*
? ? ?* @param filePath 要處理的圖片路徑
? ? ?* @param fileOutputPath 處理后的圖片輸出路徑
? ? ?* @throws IOException
? ? ?*/
? ? public static void opening(String filePath, String fileOutputPath) throws IOException {
? ? ? ? File file = new File(filePath);
? ? ? ? BufferedImage bi = ImageIO.read(file);
? ? ? ? // 獲取當前圖片的高,寬,ARGB
? ? ? ? int h = bi.getHeight();
? ? ? ? int w = bi.getWidth();
? ? ? ? int arr[][] = new int[w][h];
? ? ? ? // 獲取圖片每一像素點的灰度值
? ? ? ? for (int i = 0; i < w; i++) {
? ? ? ? ? ? for (int j = 0; j < h; j++) {
? ? ? ? ? ? ? ? // getRGB()返回默認的RGB顏色模型(十進制)
? ? ? ? ? ? ? ? arr[i][j] = getImageGray(bi.getRGB(i, j));// 該點的灰度值
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? int black = new Color(0, 0, 0).getRGB();
? ? ? ? int white = new Color(255, 255, 255).getRGB();
? ? ? ? BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
? ? ? ? // 臨時存儲腐蝕后的各個點的亮度
? ? ? ? int temp[][] = new int[w][h];
? ? ? ? // 1.先進行腐蝕操作
? ? ? ? for (int i = 0; i < w; i++) {
? ? ? ? ? ? for (int j = 0; j < h; j++) {
? ? ? ? ? ? ? ? /*
? ? ? ? ? ? ? ? ?* 為0表示改點和周圍8個點都是黑,則該點腐蝕操作后為黑
? ? ? ? ? ? ? ? ?* 由于公司圖片態(tài)模糊,完全達到9個點全為黑的點太少,最后效果很差,故改為了小于30
? ? ? ? ? ? ? ? ?* (寫30的原因是,當只有一個點為白,即總共255,調(diào)用getGray方法后得到255/9 = 28)
? ? ? ? ? ? ? ? ?*/
? ? ? ? ? ? ? ? if (getGray(arr, i, j, w, h) < 30) {
? ? ? ? ? ? ? ? ? ? temp[i][j] = 0;
? ? ? ? ? ? ? ? } else{
? ? ? ? ? ? ? ? ? ? temp[i][j] = 255;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? // 2.再進行膨脹操作
? ? ? ? for (int i = 0; i < w; i++) {
? ? ? ? ? ? for (int j = 0; j < h; j++) {
? ? ? ? ? ? ? ? bufferedImage.setRGB(i, j, white);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? for (int i = 0; i < w; i++) {
? ? ? ? ? ? for (int j = 0; j < h; j++) {
? ? ? ? ? ? ? ? // 為0表示改點和周圍8個點都是黑,則該點腐蝕操作后為黑
? ? ? ? ? ? ? ? if (temp[i][j] == 0) {
? ? ? ? ? ? ? ? ? ? bufferedImage.setRGB(i, j, black);
? ? ? ? ? ? ? ? ? ? if(i > 0) {
? ? ? ? ? ? ? ? ? ? ? ? bufferedImage.setRGB(i-1, j, black);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (j > 0) {
? ? ? ? ? ? ? ? ? ? ? ? bufferedImage.setRGB(i, j-1, black);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (i > 0 && j > 0) {
? ? ? ? ? ? ? ? ? ? ? ? bufferedImage.setRGB(i-1, j-1, black);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (j < h-1) {
? ? ? ? ? ? ? ? ? ? ? ? bufferedImage.setRGB(i, j+1, black);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (i < w-1) {
? ? ? ? ? ? ? ? ? ? ? ? bufferedImage.setRGB(i+1, j, black);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (i < w-1 && j > 0) {
? ? ? ? ? ? ? ? ? ? ? ? bufferedImage.setRGB(i+1, j-1, black);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (i < w-1 && j < h-1) {
? ? ? ? ? ? ? ? ? ? ? ? bufferedImage.setRGB(i+1, j+1, black);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (i > 0 && j < h-1) {
? ? ? ? ? ? ? ? ? ? ? ? bufferedImage.setRGB(i-1, j+1, black);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? ImageIO.write(bufferedImage, "jpg", new File(fileOutputPath));
? ? }

? ? public static void main(String[] args) {
? ? ? ? String fullPath="E:\\weijianxing\\img\\微信圖片_20201202160240.jpg";
? ? ? ? String newPath="E:\\weijianxing\\img\\1new_微信圖片_20201202160240.jpg";
? ? ? ? try {
? ? ? ? ? ? ImageOptimizationUtil.binarization(fullPath,newPath);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}

可以手動測試,然后對改代碼的部分進行調(diào)正對應的參數(shù)-- gray變量里的計算進行灰度調(diào)整

private static int getImageGray(int rgb) {
? ? ? ? String argb = Integer.toHexString(rgb);// 將十進制的顏色值轉為十六進制
? ? ? ? // argb分別代表透明,紅,綠,藍 分別占16進制2位
? ? ? ? int r = Integer.parseInt(argb.substring(2, 4), 16);// 后面參數(shù)為使用進制
? ? ? ? int g = Integer.parseInt(argb.substring(4, 6), 16);
? ? ? ? int b = Integer.parseInt(argb.substring(6, 8), 16);
? ? ? ? int gray = (int) (r*0.28 + g*0.95 + b*0.11);
? ? ? ? return gray;
? ? }

等調(diào)整之后,在對圖片進行識別即可。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 談談對Java多態(tài)性的一點理解

    談談對Java多態(tài)性的一點理解

    多態(tài)就是指程序中定義的引用變量所指向的具體類型和通過該引用變量發(fā)出的方法調(diào)用在編程時并不確定,而是在程序運行期間才確定,即一個引用變量倒底會指向哪個類的實例對象,該引用變量發(fā)出的方法調(diào)用到底是哪個類中實現(xiàn)的方法,必須在由程序運行期間才能決定
    2017-08-08
  • 解決java頁面URL地址傳輸參數(shù)亂碼的方法

    解決java頁面URL地址傳輸參數(shù)亂碼的方法

    這篇文章主要介紹了解決java頁面URL地址傳輸參數(shù)亂碼的方法,URL地址參數(shù)亂碼問題,算是老話重談了吧!需要的朋友可以參考下
    2015-09-09
  • HashMap底層實現(xiàn)原理詳解

    HashMap底層實現(xiàn)原理詳解

    這篇文章主要介紹了HashMap底層實現(xiàn)原理詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • synchronized背后的monitor鎖實現(xiàn)詳解

    synchronized背后的monitor鎖實現(xiàn)詳解

    這篇文章主要為大家介紹了synchronized背后的monitor鎖實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • 什么是jsoup及jsoup的使用

    什么是jsoup及jsoup的使用

    jsoup是一款基于Java的HTML解析器,它提供了一套非常省力的API,不但能直接解析某個URL地址、HTML文本內(nèi)容,而且還能通過類似于DOM、CSS或者jQuery的方法來操作數(shù)據(jù),所以?jsoup?也可以被當做爬蟲工具使用,這篇文章主要介紹了什么是jsoup及jsoup的使用,需要的朋友可以參考下
    2023-10-10
  • Spring Cloud實戰(zhàn)技巧之使用隨機端口

    Spring Cloud實戰(zhàn)技巧之使用隨機端口

    這篇文章主要給大家介紹了關于Spring Cloud實戰(zhàn)技巧之使用隨機端口的相關資料,文中介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面跟著小編一起來學習學習吧。
    2017-06-06
  • MyBatis傳入?yún)?shù)為List對象的實現(xiàn)

    MyBatis傳入?yún)?shù)為List對象的實現(xiàn)

    這篇文章主要介紹了MyBatis傳入?yún)?shù)為List對象的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • java如何導出insert語句并生成sql腳本

    java如何導出insert語句并生成sql腳本

    這篇文章主要介紹了java導出insert語句并生成sql腳本的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java稀疏數(shù)組的應用實踐

    Java稀疏數(shù)組的應用實踐

    本文主要介紹了Java稀疏數(shù)組的應用實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • java基礎的詳細了解第五天

    java基礎的詳細了解第五天

    這篇文章對Java編程語言的基礎知識作了一個較為全面的匯總,在這里給大家分享一下。需要的朋友可以參考,希望能給你帶來幫助
    2021-08-08

最新評論