Java Code Examples for com.google.zxing.qrcode.QRCodeReader#decode()
The following examples show how to use
com.google.zxing.qrcode.QRCodeReader#decode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QrCodeCreateUtil.java From java-study with Apache License 2.0 | 6 votes |
/** * 读二维码并输出携带的信息 */ public static void readQrCode(InputStream inputStream) throws IOException { //从输入流中获取字符串信息 BufferedImage image = ImageIO.read(inputStream); //将图像转换为二进制位图源 LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); Result result = null; try { result = reader.decode(bitmap); } catch (ReaderException e) { e.printStackTrace(); } System.out.println(result.getText()); }
Example 2
Source File: ZxingUtils.java From tysq-android with GNU General Public License v3.0 | 6 votes |
/** * 解析QR图内容 * * @param imageView * @return */ public static String readImage(ImageView imageView) { String content = null; Map<DecodeHintType, String> hints = new HashMap<>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 获得待解析的图片 Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); RGBLuminanceSource source = new RGBLuminanceSource(bitmap); BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { Result result = reader.decode(bitmap1, hints); // 得到解析后的文字 content = result.getText(); } catch (Exception e) { e.printStackTrace(); } return content; }
Example 3
Source File: QRCodeEncoderDecoder.java From bither-desktop-java with Apache License 2.0 | 6 votes |
public static String decode(BufferedImage image) { // convert the image to a binary bitmap source LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); // decode the barcode QRCodeReader reader = new QRCodeReader(); try { @SuppressWarnings("rawtypes") Hashtable hints = new Hashtable(); Result result = reader.decode(bitmap, hints); return result.getText(); } catch (ReaderException e) { // the data is improperly formatted } return ""; }
Example 4
Source File: QRCodeEncoderDecoder.java From bither-desktop-java with Apache License 2.0 | 6 votes |
public static String decode(BufferedImage image) { // convert the image to a binary bitmap source LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); // decode the barcode QRCodeReader reader = new QRCodeReader(); try { @SuppressWarnings("rawtypes") Hashtable hints = new Hashtable(); Result result = reader.decode(bitmap, hints); return result.getText(); } catch (ReaderException e) { // the data is improperly formatted } return ""; }
Example 5
Source File: QrUtils.java From Tesseract-OCR-Scanner with Apache License 2.0 | 5 votes |
/** * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency, reuse the same reader * objects from one decode to the next. */ public static Result decodeImage(byte[] data, int width, int height) { // 处理 Result result = null; try { Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false); /** * HybridBinarizer算法使用了更高级的算法,但使用GlobalHistogramBinarizer识别效率确实比HybridBinarizer要高一些。 * * GlobalHistogram算法:(http://kuangjianwei.blog.163.com/blog/static/190088953201361015055110/) * * 二值化的关键就是定义出黑白的界限,我们的图像已经转化为了灰度图像,每个点都是由一个灰度值来表示,就需要定义出一个灰度值,大于这个值就为白(0),低于这个值就为黑(1)。 * 在GlobalHistogramBinarizer中,是从图像中均匀取5行(覆盖整个图像高度),每行取中间五分之四作为样本;以灰度值为X轴,每个灰度值的像素个数为Y轴建立一个直方图, * 从直方图中取点数最多的一个灰度值,然后再去给其他的灰度值进行分数计算,按照点数乘以与最多点数灰度值的距离的平方来进行打分,选分数最高的一个灰度值。接下来在这两个灰度值中间选取一个区分界限, * 取的原则是尽量靠近中间并且要点数越少越好。界限有了以后就容易了,与整幅图像的每个点进行比较,如果灰度值比界限小的就是黑,在新的矩阵中将该点置1,其余的就是白,为0。 */ BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source)); // BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader2 = new QRCodeReader(); result = reader2.decode(bitmap1, hints); } catch (ReaderException e) { } return result; }
Example 6
Source File: QrUtils.java From Mobike with Apache License 2.0 | 5 votes |
/** * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency, reuse the same reader * objects from one decode to the next. */ public static Result decodeImage(byte[] data, int width, int height) { // 处理 Result result = null; try { Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false); /** * HybridBinarizer算法使用了更高级的算法,但使用GlobalHistogramBinarizer识别效率确实比HybridBinarizer要高一些。 * * GlobalHistogram算法:(http://kuangjianwei.blog.163.com/blog/static/190088953201361015055110/) * * 二值化的关键就是定义出黑白的界限,我们的图像已经转化为了灰度图像,每个点都是由一个灰度值来表示,就需要定义出一个灰度值,大于这个值就为白(0),低于这个值就为黑(1)。 * 在GlobalHistogramBinarizer中,是从图像中均匀取5行(覆盖整个图像高度),每行取中间五分之四作为样本;以灰度值为X轴,每个灰度值的像素个数为Y轴建立一个直方图, * 从直方图中取点数最多的一个灰度值,然后再去给其他的灰度值进行分数计算,按照点数乘以与最多点数灰度值的距离的平方来进行打分,选分数最高的一个灰度值。接下来在这两个灰度值中间选取一个区分界限, * 取的原则是尽量靠近中间并且要点数越少越好。界限有了以后就容易了,与整幅图像的每个点进行比较,如果灰度值比界限小的就是黑,在新的矩阵中将该点置1,其余的就是白,为0。 */ BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source)); // BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader2 = new QRCodeReader(); result = reader2.decode(bitmap1, hints); } catch (ReaderException e) { } return result; }
Example 7
Source File: QrUtils.java From QrCodeScanner with GNU General Public License v3.0 | 5 votes |
/** * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency, reuse the same reader * objects from one decode to the next. */ public static Result decodeImage(byte[] data, int width, int height) { // 处理 Result result = null; try { Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false); /** * HybridBinarizer算法使用了更高级的算法,但使用GlobalHistogramBinarizer识别效率确实比HybridBinarizer要高一些。 * * GlobalHistogram算法:(http://kuangjianwei.blog.163.com/blog/static/190088953201361015055110/) * * 二值化的关键就是定义出黑白的界限,我们的图像已经转化为了灰度图像,每个点都是由一个灰度值来表示,就需要定义出一个灰度值,大于这个值就为白(0),低于这个值就为黑(1)。 * 在GlobalHistogramBinarizer中,是从图像中均匀取5行(覆盖整个图像高度),每行取中间五分之四作为样本;以灰度值为X轴,每个灰度值的像素个数为Y轴建立一个直方图, * 从直方图中取点数最多的一个灰度值,然后再去给其他的灰度值进行分数计算,按照点数乘以与最多点数灰度值的距离的平方来进行打分,选分数最高的一个灰度值。接下来在这两个灰度值中间选取一个区分界限, * 取的原则是尽量靠近中间并且要点数越少越好。界限有了以后就容易了,与整幅图像的每个点进行比较,如果灰度值比界限小的就是黑,在新的矩阵中将该点置1,其余的就是白,为0。 */ BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source)); // BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader2 = new QRCodeReader(); result = reader2.decode(bitmap1, hints); } catch (ReaderException e) { } return result; }
Example 8
Source File: ScannerActivity.java From talk-android with MIT License | 5 votes |
protected Result scanningImage(String path) { if (StringUtil.isBlank(path)) { return null; } // DecodeHintType 和EncodeHintType Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 先获取原大小 scanBitmap = BitmapFactory.decodeFile(path, options); options.inJustDecodeBounds = false; // 获取新的大小 int sampleSize = (int) (options.outHeight / (float) 200); if (sampleSize <= 0) sampleSize = 1; options.inSampleSize = sampleSize; scanBitmap = BitmapFactory.decodeFile(path, options); int width = scanBitmap.getWidth(), height = scanBitmap.getHeight(); int[] pixels = new int[width * height]; scanBitmap.getPixels(pixels, 0, width, 0, 0, width, height); scanBitmap.recycle(); scanBitmap = null; RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { return reader.decode(bitmap1, hints); } catch (Exception e) { e.printStackTrace(); } return null; }
Example 9
Source File: MyQRCodeUtils.java From spring-boot with Apache License 2.0 | 5 votes |
/** * 解码 QRCode 图片,解析出其内容 * * @param imageURI QRCode 图片 URI * @return 解析后的内容 * @throws IOException */ public static String decodeQRCodeImage(URI imageURI) throws IOException { BufferedImage bufferedImage = ImageReader.readImage(imageURI); LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { Result result = reader.decode(bitmap); return result.getText(); } catch (ReaderException e) { e.printStackTrace(); } return ""; }
Example 10
Source File: DecodeUtil.java From react-native-smart-barcode with MIT License | 5 votes |
public static String getStringFromQRCode(String path) { String httpString = null; Bitmap bmp = convertToBitmap(path); byte[] data = getYUV420sp(bmp.getWidth(), bmp.getHeight(), bmp); // 处理 try { Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); // hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, bmp.getWidth(), bmp.getHeight(), 0, 0, bmp.getWidth(), bmp.getHeight(), false); BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader2= new QRCodeReader(); Result result = reader2.decode(bitmap1, hints); httpString = result.getText(); } catch (Exception e) { e.printStackTrace(); } bmp.recycle(); bmp = null; return httpString; }
Example 11
Source File: QrCodeHelper.java From FamilyChat with Apache License 2.0 | 5 votes |
/** * 直接识别二维码位图 * * @param bitmap 二维码位图 * @return 二维码包含的内容 */ public static String recognizeQrCode(Bitmap bitmap) { if (bitmap == null) return null; Hashtable<DecodeHintType, String> hints = new Hashtable<>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码 //获取图片的像素存入到数组 int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()]; bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); RGBLuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), pixels); BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { Result result = reader.decode(bitmap1, hints); if (reader == null) { return null; } else { String str = result.toString(); boolean ISO = Charset.forName("ISO-8859-1").newEncoder().canEncode(str); if (ISO) str = new String(str.getBytes("ISO-8859-1"), "GB2312"); return str; } } catch (Exception e) { Log.e("QrCodeHelper", "recognizeQrCode fail:" + e.toString()); } return null; }
Example 12
Source File: ScannerManager.java From attendee-checkin with Apache License 2.0 | 5 votes |
private String decode(byte[] data, int width, int height) { ScannerManager manager = mManager.get(); if (manager == null) { return null; } Rect rect = manager.getFramingRectInPreview(); PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.right, rect.bottom, false); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { Result result = reader.decode(bitmap, mHints); return result.getText(); } catch (ReaderException e) { // Ignore as we will repeatedly decode the preview frame return null; } }
Example 13
Source File: HttpAndTCPSameJVMTest.java From blynk-server with GNU General Public License v3.0 | 5 votes |
@Test public void testQRWorks() throws Exception { HttpGet getRequest = new HttpGet(httpServerUrl + clientPair.token + "/qr"); String cloneToken; try (CloseableHttpResponse response = httpclient.execute(getRequest)) { assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals("image/png", response.getFirstHeader("Content-Type").getValue()); byte[] data = EntityUtils.toByteArray(response.getEntity()); assertNotNull(data); //get the data from the input stream BufferedImage image = ImageIO.read(new ByteArrayInputStream(data)); //convert the image to a binary bitmap source LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); Result result = reader.decode(bitmap); String resultString = result.getText(); assertTrue(resultString.startsWith("blynk://token/clone/")); assertTrue(resultString.endsWith("?server=127.0.0.1&port=10443")); cloneToken = resultString.substring( resultString.indexOf("blynk://token/clone/") + "blynk://token/clone/".length(), resultString.indexOf("?server=127.0.0.1&port=10443")); assertEquals(32, cloneToken.length()); } clientPair.appClient.send("getProjectByCloneCode " + cloneToken); DashBoard dashBoard = clientPair.appClient.parseDash(1); assertEquals("My Dashboard", dashBoard.name); }
Example 14
Source File: RCTQRCodeLocalImage.java From react-native-qrcode-local-image with MIT License | 4 votes |
@ReactMethod public void decode(String path, Callback callback) { Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 先获取原大小 options.inJustDecodeBounds = false; // 获取新的大小 int sampleSize = (int) (options.outHeight / (float) 200); if (sampleSize <= 0) sampleSize = 1; options.inSampleSize = sampleSize; Bitmap scanBitmap = null; if (path.startsWith("http://")||path.startsWith("https://")) { scanBitmap = this.getbitmap(path); } else { scanBitmap = BitmapFactory.decodeFile(path, options); } if (scanBitmap == null) { callback.invoke("cannot load image"); return; } int[] intArray = new int[scanBitmap.getWidth()*scanBitmap.getHeight()]; scanBitmap.getPixels(intArray, 0, scanBitmap.getWidth(), 0, 0, scanBitmap.getWidth(), scanBitmap.getHeight()); RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), intArray); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { Result result = reader.decode(bitmap, hints); if (result == null) { callback.invoke("image format error"); } else { callback.invoke(null, result.toString()); } } catch (Exception e) { callback.invoke("decode error"); } }
Example 15
Source File: RequiredActionsTest.java From keycloak with Apache License 2.0 | 4 votes |
private void testBarcodeOtp() throws Exception { assumeFalse(driver instanceof HtmlUnitDriver); // HtmlUnit browser cannot take screenshots TakesScreenshot screenshotDriver = (TakesScreenshot) driver; QRCodeReader qrCodeReader = new QRCodeReader(); initiateRequiredAction(otpSetupPage); otpSetupPage.localeDropdown().selectAndAssert(CUSTOM_LOCALE_NAME); otpSetupPage.clickManualMode(); otpSetupPage.clickBarcodeMode(); assertTrue(otpSetupPage.isBarcodePresent()); assertFalse(otpSetupPage.isSecretKeyPresent()); assertTrue(otpSetupPage.feedbackMessage().isWarning()); assertEquals("You need to set up Mobile Authenticator to activate your account.", otpSetupPage.feedbackMessage().getText()); // empty input otpSetupPage.submit(); assertTrue(otpSetupPage.feedbackMessage().isError()); assertEquals("Please specify authenticator code.", otpSetupPage.feedbackMessage().getText()); // take a screenshot of the QR code byte[] screenshot = screenshotDriver.getScreenshotAs(OutputType.BYTES); BufferedImage screenshotImg = ImageIO.read(new ByteArrayInputStream(screenshot)); BinaryBitmap screenshotBinaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(screenshotImg))); Result qrCode = qrCodeReader.decode(screenshotBinaryBitmap); // parse the QR code string Pattern qrUriPattern = Pattern.compile("^otpauth:\\/\\/(?<type>.+)\\/(?<realm>.+):(?<user>.+)\\?secret=(?<secret>.+)&digits=(?<digits>.+)&algorithm=(?<algorithm>.+)&issuer=(?<issuer>.+)&(?:period=(?<period>.+)|counter=(?<counter>.+))$"); Matcher qrUriMatcher = qrUriPattern.matcher(qrCode.getText()); assertTrue(qrUriMatcher.find()); // extract data String type = qrUriMatcher.group("type"); String realm = qrUriMatcher.group("realm"); String user = qrUriMatcher.group("user"); String secret = qrUriMatcher.group("secret"); int digits = Integer.parseInt(qrUriMatcher.group("digits")); String algorithm = qrUriMatcher.group("algorithm"); String issuer = qrUriMatcher.group("issuer"); Integer period = type.equals(TOTP) ? Integer.parseInt(qrUriMatcher.group("period")) : null; Integer counter = type.equals(HOTP) ? Integer.parseInt(qrUriMatcher.group("counter")) : null; RealmRepresentation realmRep = testRealmResource().toRepresentation(); String expectedRealmName = realmRep.getDisplayName() != null && !realmRep.getDisplayName().isEmpty() ? realmRep.getDisplayName() : realmRep.getRealm(); // basic assertations assertEquals(QR_CODE, qrCode.getBarcodeFormat()); assertEquals(expectedRealmName, realm); assertEquals(expectedRealmName, issuer); assertEquals(testUser.getUsername(), user); // the actual test testOtp(type, algorithm, digits, period, counter, secret); }
Example 16
Source File: EncoderDecoder.java From RipplePower with Apache License 2.0 | 4 votes |
public String decode(BufferedImage image) throws IOException { LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); Result result = reader.decode(bitmap, hints); return result.getText(); } catch (ReaderException e) { throw new IOException(e); } }
Example 17
Source File: QRCodeUtils.java From qrcodecore with GNU General Public License v2.0 | 2 votes |
/** * 解析二维码 * @param binaryBitmap 二维码图类 * @return 解析信息 * @throws FormatException * @throws ChecksumException * @throws NotFoundException */ private static Result readQRcode(BinaryBitmap binaryBitmap, Map<DecodeHintType, Object> decodeHints) throws FormatException, ChecksumException, NotFoundException { QRCodeReader reader = new QRCodeReader(); return reader.decode(binaryBitmap, decodeHints); }