public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\Administrator\\Desktop\\a.png");
BufferedImage image = ImageIO.read(file);
HashMap<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatReader reader = new MultiFormatReader();
Result result = null;
try {
result = reader.decode(new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(image))), hints);
} catch (NotFoundException e) {
e.printStackTrace();
BufferedImage image1 = ImageOptimizationUtil.opening(image);
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
hints.put(DecodeHintType.PURE_BARCODE,Boolean.TRUE);
try {
result = reader.decode(new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(image1))),hints);
} catch (NotFoundException ex) {
throw new RuntimeException(ex);
}
}
if (result != null) {
System.out.println(result.getText());
}
}
package com.ruoyi.common.utils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class ImageOptimizationUtil {
public static int YZ = 150;
public static BufferedImage binarization(BufferedImage bi) throws IOException {
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++) {
arr[i][j] = getImageGray(bi.getRGB(i, j));
}
}
BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
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);
}
}
}
return bufferedImage;
}
private static int getImageGray(int rgb) {
String argb = Integer.toHexString(rgb);
int r = Integer.parseInt(argb.substring(2, 4), 16);
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;
}
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;
}
public static BufferedImage opening(BufferedImage bi) throws IOException {
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++) {
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];
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
if (getGray(arr, i, j, w, h) < 30) {
temp[i][j] = 0;
} else{
temp[i][j] = 255;
}
}
}
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++) {
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);
}
}
}
}
return bufferedImage;
}
}