In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to identify QR code pictures by java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Required maven dependency
Com.google.zxing javase 3.2.1 com.google.zxing core 3.3.3
Implemented java class
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 / * function: QR code recognition (picture) * class name: QRCodeUtils * / public class QRCodeUtils {/ * parse the QR code. This method parses the QR code picture of a path * path: picture 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 hints = new HashMap (); hints.put (DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = new MultiFormatReader (). Decode (binaryBitmap, hints); / / decode System.out.println ("content in the picture:") System.out.println ("content:" + result.getText ()); content = result.getText ();} catch (IOException e) {e.printStackTrace () } catch (NotFoundException e) {/ / it is judged here that if the picture with LOGO cannot be recognized, add an attribute try {image = ImageIO.read (new File (path)); LuminanceSource source = new BufferedImageLuminanceSource (image); Binarizer binarizer = new HybridBinarizer (source); BinaryBitmap binaryBitmap = new BinaryBitmap (binarizer) Map hints = new HashMap (); / / set encoding format hints.put (DecodeHintType.CHARACTER_SET, "UTF-8"); / / set optimization precision hints.put (DecodeHintType.TRY_HARDER, Boolean.TRUE) / / set complex mode on (I can recognize the Wechat's QR code in this way) hints.put (DecodeHintType.PURE_BARCODE,Boolean.TYPE); Result result = new MultiFormatReader (). Decode (binaryBitmap, hints); / / decode System.out.println ("content in the picture:") System.out.println ("content:" + result.getText ()); content = result.getText ();} catch (IOException e) {e.printStackTrace ();} catch (NotFoundException e) {e.printStackTrace ();}} return content;}}
test
Public static void main (String [] args) {deEncodeByPath ("D:\\ Users/admin/Desktop/erweima/timg (5) .jpg"); / / QR code image path}
Output result:
The content in the picture:
Content: http://qrcode.online
If the above can not be recognized, then you need to process the image once, and then identify it. Here is a tool class for tuning the image.
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 {/ / threshold 0-255 public static int YZ = 150 / * * Image binarization processing * * @ param filePath Image path to be processed * @ Image output path after param fileOutputPath processing * / public static void binarization (String filePath, String fileOutputPath) throws IOException {File file = new File (filePath); BufferedImage bi = ImageIO.read (file) / / get the height and width of the current picture, ARGB int h = bi.getHeight (); int w = bi.getWidth (); int arr [] [] = new int [w] [h]; / / get the gray value for (int I = 0; I) of each pixel of the picture
< 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)); grayscale processing of} / * * image * using floating-point algorithm: Gray = Run0.3 + Gloss0.59 + Bron0.11 * * @ param rgb the RGB value of this point * @ return returns the processed grayscale value * / private static int getImageGray (int rgb) {String argb = Integer.toHexString (rgb); / / convert the decimal color value to hexadecimal / / argb represents transparency, red, green and blue, respectively, int r = Integer.parseInt (argb.substring (2,4), 16) / / the following parameters are: using binary int g = Integer.parseInt (argb.substring (4,6), 16); int b = Integer.parseInt (argb.substring (6,8), 16); int gray = (int) (rang 0.28 + grub 0.95 + bang 0.11); return gray } / * add 8 surrounding grayscale values and divide by 9 Calculate its relative gray value * * @ param gray * @ param x Abscissa of the point to be calculated * @ param y the ordinate of the point to be calculated * @ param w image width * @ param h image height * @ 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 = 0255: 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-1255: gray [x + 1] [y + 1]) Open operation of return rs / 9;} / * binary image: first corrode and then inflate (used to remove small black spots of the image) * * @ param filePath image path to be processed * @ param fileOutputPath image output path * @ throws IOException * / public static void opening (String filePath, String fileOutputPath) throws IOException {File file = new File (filePath) BufferedImage bi = ImageIO.read (file); / / get the height and width of the current picture, ARGB int h = bi.getHeight (); int w = bi.getWidth (); int arr [] [] = new int [w] [h]; / get the gray value for (int I = 0; I) of each pixel of the picture
< 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个点都是黑,则该点腐蚀操作后为黑 * 由于公司图片态模糊,完全达到9个点全为黑的点太少,最后效果很差,故改为了小于30 * (写30的原因是,当只有一个点为白,即总共255,调用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 (iMur1, j, black);} if (j > 0) {bufferedImage.setRGB (I, jmur1, black) } if (I > 0 & & j > 0) {bufferedImage.setRGB (imur1, jmur1, 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 (iTun1, jmur1, black);} if (I
< w-1 && j < h-1) { bufferedImage.setRGB(i+1, j+1, black); } if (i >0 & & j < hmur1) {bufferedImage.setRGB (iMuth1, juli1, black);} ImageIO.write (bufferedImage, "jpg", new File (fileOutputPath)) } public static void main (String [] args) {String fullPath= "E:\\ weijianxing\\ img\\ Wechat picture _ 20201202160240.jpg"; String newPath= "E:\\ weijianxing\\ img\\ 1new_ Wechat picture _ 20201202160240.jpg"; try {ImageOptimizationUtil.binarization (fullPath,newPath);} catch (IOException e) {e.printStackTrace ();}
You can test it manually, and then adjust the corresponding parameter-- the calculation in the gray variable-- to adjust the grayscale of the changed code.
Private static int getImageGray (int rgb) {String argb = Integer.toHexString (rgb); / / convert the decimal color value to hexadecimal / / argb represents transparent, red, green and blue respectively 2 bits int r = Integer.parseInt (argb.substring (2,4), 16); / / the latter parameter is to use the binary int g = Integer.parseInt (argb.substring (4,6), 16) Int b = Integer.parseInt (argb.substring (6,8), 16); int gray = (int) (rang 0.28 + gr 0.95 + bang 0.11); return gray;}
After the adjustment, you can identify the picture.
This is the end of the content of "how to identify the picture function of QR code by java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.