public static String decodeQr(String filePath) { String retStr = ""; if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) { return "图片路径为空!"; } try { BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath)); LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap bitmap = new BinaryBitmap(binarizer); HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>(); hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap); retStr = result.getText(); } catch (Exception e) { logger.error("", e); } return retStr; }
public static String decodeQr(String filePath) { String retStr = ""; if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) { return "图片路径为空!"; } try { BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath)); LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap bitmap = new BinaryBitmap(binarizer); HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>(); hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap); retStr = result.getText(); } catch (Exception e) { e.printStackTrace(); } return retStr; }
/** * 读取二维码 * @param qrCodeFile * @return */ public String readQrCode(File qrCodeFile){ String ret = null; try { QRCodeReader reader = new QRCodeReader(); BufferedImage image = ImageIO.read(qrCodeFile); LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap imageBinaryBitmap = new BinaryBitmap(binarizer); Result result = reader.decode(imageBinaryBitmap); ret = result.getText(); } catch (IOException |NotFoundException | ChecksumException | FormatException e) { Exceptions.printException(e); } return ret; }
private static void parse() throws IOException, NotFoundException, ChecksumException, FormatException { BufferedImage image = ImageReader.readImage(Paths.get("d:/qr.png").toUri()); LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer bin = new HybridBinarizer(source); BinaryBitmap bitmap = new BinaryBitmap(bin); Result result = new QRCodeReader().decode(bitmap); System.out.println(result.toString()); }
private Bitmap binarization(Bitmap bitmap, int lowColor, int highColor) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int pixels[] = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); LuminanceSource source = new RGBLuminanceSource(width, height, pixels); Binarizer binarizer = new HybridBinarizer(source); Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); try { BitMatrix matrix = binarizer.getBlackMatrix(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (matrix.get(j, i)) { result.setPixel(j, i, highColor); } else { result.setPixel(j, i, lowColor); } } } } catch (NotFoundException e) { e.printStackTrace(); } return result; }
/** * 二值化 * * @param bitmap * @return */ private Bitmap binarization(Bitmap bitmap, int lowColor, int highStartColor, int highEndColor) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int pixels[] = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); LuminanceSource source = new RGBLuminanceSource(width, height, pixels); Binarizer binarizer = new HybridBinarizer(source); Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); try { BitMatrix matrix = binarizer.getBlackMatrix(); int highColor; for (int i = 0; i < height; i++) { // highColor = getGradientColor(highStartColor, highEndColor, i / (float) height); highColor = getGradientColorByCurve(highStartColor, highEndColor, 0, height, i); for (int j = 0; j < width; j++) { if (matrix.get(j, i)) { result.setPixel(j, i, highColor); } else { result.setPixel(j, i, lowColor); } } } } catch (NotFoundException e) { e.printStackTrace(); } return result; }
@Override public Binarizer createBinarizer(LuminanceSource source) { return new GlobalHistogramBinarizer(source); }
@Override public Binarizer createBinarizer(LuminanceSource source) { return new HybridBinarizer(source); }
public Binarizer createBinarizer(LuminanceSource source) { return new GlobalHistogramBinarizer(source); }
public Binarizer createBinarizer(LuminanceSource source) { return new HybridBinarizer(source); }