com.google.zxing.qrcode.QRCodeWriter Java Examples
The following examples show how to use
com.google.zxing.qrcode.QRCodeWriter.
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: TotpUtils.java From keycloak with Apache License 2.0 | 8 votes |
public static String qrCode(String totpSecret, RealmModel realm, UserModel user) { try { String keyUri = realm.getOTPPolicy().getKeyURI(realm, user, totpSecret); int width = 246; int height = 246; QRCodeWriter writer = new QRCodeWriter(); final BitMatrix bitMatrix = writer.encode(keyUri, BarcodeFormat.QR_CODE, width, height); ByteArrayOutputStream bos = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(bitMatrix, "png", bos); bos.close(); return Base64.encodeBytes(bos.toByteArray()); } catch (Exception e) { throw new RuntimeException(e); } }
Example #2
Source File: QrCode.java From mollyim-android with GNU General Public License v3.0 | 7 votes |
public static @NonNull Bitmap create(String data) { try { BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, 512, 512); Bitmap bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888); for (int y = 0; y < result.getHeight(); y++) { for (int x = 0; x < result.getWidth(); x++) { if (result.get(x, y)) { bitmap.setPixel(x, y, Color.BLACK); } } } return bitmap; } catch (WriterException e) { Log.w(TAG, e); return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888); } }
Example #3
Source File: AttestationActivity.java From Auditor with MIT License | 6 votes |
private Bitmap createQrCode(final byte[] contents) { final BitMatrix result; try { final QRCodeWriter writer = new QRCodeWriter(); final Map<EncodeHintType,Object> hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.ISO_8859_1); final int size = Math.min(imageView.getWidth(), imageView.getHeight()); result = writer.encode(new String(contents, StandardCharsets.ISO_8859_1), BarcodeFormat.QR_CODE, size, size, hints); } catch (WriterException e) { throw new RuntimeException(e); } final int width = result.getWidth(); final int height = result.getHeight(); final int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { final int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB_565); }
Example #4
Source File: PrintItemBarcode.java From nordpos with GNU General Public License v3.0 | 6 votes |
@Override public void draw(Graphics2D g, int x, int y, int width) { Graphics2D g2d = (Graphics2D) g; AffineTransform oldt = g2d.getTransform(); g2d.translate(x - 10 + (width - (int) (m_iWidth * scale)) / 2, y + 10); g2d.scale(scale, scale); try { if (m_qrMatrix != null) { com.google.zxing.Writer writer = new QRCodeWriter(); m_qrMatrix = writer.encode(m_sCode, com.google.zxing.BarcodeFormat.QR_CODE, m_iWidth, m_iHeight); g2d.drawImage(MatrixToImageWriter.toBufferedImage(m_qrMatrix), null, 0, 0); } else if (m_barcode != null) { m_barcode.generateBarcode(new Java2DCanvasProvider(g2d, 0), m_sCode); } } catch (IllegalArgumentException | WriterException ex) { g2d.drawRect(0, 0, m_iWidth, m_iHeight); g2d.drawLine(0, 0, m_iWidth, m_iHeight); g2d.drawLine(m_iWidth, 0, 0, m_iHeight); } g2d.setTransform(oldt); }
Example #5
Source File: QRCodeHelper.java From AndroidWallet with GNU General Public License v3.0 | 6 votes |
public static Bitmap generateBitmap(String content, int width, int height) { int margin = 5; //自定义白边边框宽度 QRCodeWriter qrCodeWriter = new QRCodeWriter(); Hashtable hints = new Hashtable<>(); hints.put(EncodeHintType.MARGIN, 0); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); try { BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints); // encode = updateBit(encode, margin); //生成新的bitMatrix int[] pixels = new int[width * height]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (encode.get(j, i)) { pixels[i * width + j] = 0x00000000; } else { pixels[i * width + j] = 0xffffffff; } } } return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565); } catch (WriterException e) { e.printStackTrace(); } return null; }
Example #6
Source File: QRCodeEngine.java From SlidesRemote with Apache License 2.0 | 6 votes |
public static Image encode(String data, int width, int height) { try { BitMatrix byteMatrix = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, width, height); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); bufferedImage.createGraphics(); Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics(); graphics.setColor(Color.decode("#00adb5")); graphics.fillRect(0, 0, width, height); graphics.setColor(Color.BLACK); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (byteMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } return SwingFXUtils.toFXImage(bufferedImage, null); } catch (WriterException ex) { ex.printStackTrace(); return null; } }
Example #7
Source File: BarcodeProvider.java From Conversations with GNU General Public License v3.0 | 6 votes |
public static Bitmap create2dBarcodeBitmap(String input, int size) { try { final QRCodeWriter barcodeWriter = new QRCodeWriter(); final Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); final BitMatrix result = barcodeWriter.encode(input, BarcodeFormat.QR_CODE, size, size, hints); final int width = result.getWidth(); final int height = result.getHeight(); final int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { final int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE; } } final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (final Exception e) { e.printStackTrace(); return null; } }
Example #8
Source File: ToolBarCode.java From protools with Apache License 2.0 | 6 votes |
/** * 生成带logo的二维码 * * @param content 条码文本内容 * @param width 条码宽度 * @param height 条码高度 * @param logoPath 条码中logo的路径 * @param fileType 文件类型,如png * @param savePath 保存路径 */ public static void encodeLogo(String content, int width, int height, String logoPath, String fileType, String savePath) throws IOException, WriterException { Charset charset = Charset.forName("UTF-8"); CharsetEncoder encoder = charset.newEncoder(); byte[] b = encoder.encode(CharBuffer.wrap(content)).array(); String data = new String(b, "iso8859-1"); Writer writer = new QRCodeWriter(); BitMatrix matrix = writer.encode(data, QR_CODE, width, height); MatrixToLogoImageConfig logoConfig = new MatrixToLogoImageConfig(Color.BLACK, 10); MatrixToImageWriterEx.writeToFile(matrix, fileType, savePath, logoPath, logoConfig); // BitMatrix matrix = MatrixToImageWriterEx.createQRCode(content, width, height); // MatrixToLogoImageConfig logoConfig = new MatrixToLogoImageConfig(Color.BLUE, 4); // MatrixToImageWriterEx.writeToFile(matrix, fileType, savePath, logoPath, logoConfig); }
Example #9
Source File: QRCodeController.java From eds-starter6-jpa with Apache License 2.0 | 6 votes |
@RequireAnyAuthority @RequestMapping(value = "/qr", method = RequestMethod.GET) public void qrcode(HttpServletResponse response, @AuthenticationPrincipal JpaUserDetails jpaUserDetails) throws WriterException, IOException { User user = jpaUserDetails.getUser(this.jpaQueryFactory); if (user != null && StringUtils.hasText(user.getSecret())) { response.setContentType("image/png"); String contents = "otpauth://totp/" + user.getEmail() + "?secret=" + user.getSecret() + "&issuer=" + this.appName; QRCodeWriter writer = new QRCodeWriter(); BitMatrix matrix = writer.encode(contents, BarcodeFormat.QR_CODE, 200, 200); MatrixToImageWriter.writeToStream(matrix, "PNG", response.getOutputStream()); response.getOutputStream().flush(); } }
Example #10
Source File: BarcodeProvider.java From Pix-Art-Messenger with GNU General Public License v3.0 | 6 votes |
public static Bitmap create2dBarcodeBitmap(String input, int size) { try { final QRCodeWriter barcodeWriter = new QRCodeWriter(); final Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); final BitMatrix result = barcodeWriter.encode(input, BarcodeFormat.QR_CODE, size, size, hints); final int width = result.getWidth(); final int height = result.getHeight(); final int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { final int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE; } } final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (final Exception e) { e.printStackTrace(); return null; } }
Example #11
Source File: MobileServerPreference.java From Quelea with GNU General Public License v3.0 | 6 votes |
private Image getQRImage() { if (qrImage == null) { QRCodeWriter qrCodeWriter = new QRCodeWriter(); int qrWidth = 500; int qrHeight = 500; BitMatrix byteMatrix = null; try { byteMatrix = qrCodeWriter.encode(getMLURL(), BarcodeFormat.QR_CODE, qrWidth, qrHeight); } catch (WriterException ex) { LOGGER.log(Level.WARNING, "Error writing QR code", ex); } qrImage = MatrixToImageWriter.toBufferedImage(byteMatrix); } WritableImage fxImg = new WritableImage(500, 500); SwingFXUtils.toFXImage(qrImage, fxImg); return fxImg; }
Example #12
Source File: MainActivity.java From ExamplesAndroid with Apache License 2.0 | 6 votes |
public static Bitmap encodeToQrCode(String text, int width, int height){ QRCodeWriter writer = new QRCodeWriter(); BitMatrix matrix = null; try { matrix = writer.encode(text, BarcodeFormat.QR_CODE, 100, 100); } catch (WriterException ex) { ex.printStackTrace(); } Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); for (int x = 0; x < width; x++){ for (int y = 0; y < height; y++){ bmp.setPixel(x, y, matrix.get(x,y) ? Color.BLACK : Color.WHITE); } } return bmp; }
Example #13
Source File: WechatGroupRobotImpl.java From wechatGroupRobot with GNU General Public License v3.0 | 6 votes |
public void showQrImage(String uuid) { String uid = null != uuid ? uuid : this.uuid; String url = "https://login.weixin.qq.com/qrcode/" + uid + "?t=webwx"; final File output = new File("temp.jpg"); //下载二维码 File qrImage = HttpClientUtil.doGetImage(url); //控制台显示二维码 Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hintMap.put(EncodeHintType.MARGIN, 1); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); String qrContent = readQRCode(qrImage, hintMap); QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix; try { bitMatrix = qrCodeWriter.encode(qrContent, BarcodeFormat.QR_CODE, 10, 10, hintMap); System.out.println(toAscii(bitMatrix)); } catch (WriterException e) { e.printStackTrace(); } }
Example #14
Source File: QRCodeGen.java From TVRemoteIME with GNU General Public License v2.0 | 6 votes |
public static Bitmap generateBitmap(String content, int width, int height) { QRCodeWriter qrCodeWriter = new QRCodeWriter(); Map<EncodeHintType, String> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); try { BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints); int[] pixels = new int[width * height]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (encode.get(j, i)) { pixels[i * width + j] = 0x00000000; } else { pixels[i * width + j] = 0xffffffff; } } } return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565); } catch (WriterException e) { e.printStackTrace(); } return null; }
Example #15
Source File: QrCodeCreateUtil.java From java-study with Apache License 2.0 | 6 votes |
/** * 生成包含字符串信息的二维码图片 * * @param outputStream 文件输出流路径 * @param content 二维码携带信息 * @param qrCodeSize 二维码图片大小 * @param imageFormat 二维码的格式 * @throws WriterException * @throws IOException */ public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException { //设置二维码纠错级别MAP Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别 QRCodeWriter qrCodeWriter = new QRCodeWriter(); //创建比特矩阵(位矩阵)的QR码编码的字符串 BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap); // 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点) int matrixWidth = byteMatrix.getWidth(); BufferedImage image = new BufferedImage(matrixWidth - 200, matrixWidth - 200, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, matrixWidth, matrixWidth); // 使用比特矩阵画并保存图像 graphics.setColor(Color.BLACK); for (int i = 0; i < matrixWidth; i++) { for (int j = 0; j < matrixWidth; j++) { if (byteMatrix.get(i, j)) { graphics.fillRect(i - 100, j - 100, 1, 1); } } } return ImageIO.write(image, imageFormat, outputStream); }
Example #16
Source File: QrCodeUtils.java From WiFiKeyShare with GNU General Public License v3.0 | 6 votes |
/** * Generate a QR code containing the given Wi-Fi configuration * * @param width the width of the QR code * @param wifiNetwork the Wi-Fi configuration * @return a bitmap representing the QR code * @throws WriterException if the Wi-Fi configuration cannot be represented in the QR code */ public static Bitmap generateWifiQrCode(int width, WifiNetwork wifiNetwork) throws WriterException { int height = width; com.google.zxing.Writer writer = new QRCodeWriter(); String wifiString = getWifiString(wifiNetwork); BitMatrix bitMatrix = writer.encode(wifiString, BarcodeFormat.QR_CODE, width, height); Bitmap imageBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { imageBitmap.setPixel(i, j, bitMatrix.get(i, j) ? Color.BLACK : Color.WHITE); } } return imageBitmap; }
Example #17
Source File: PMedia.java From PHONK with GNU General Public License v3.0 | 6 votes |
public Bitmap generateQRCode(String text) { Bitmap bmp = null; Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // H = 30% damage int size = 256; BitMatrix bitMatrix = null; try { bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hintMap); int width = bitMatrix.getWidth(); bmp = Bitmap.createBitmap(width, width, Bitmap.Config.RGB_565); for (int x = 0; x < width; x++) { for (int y = 0; y < width; y++) { bmp.setPixel(y, x, bitMatrix.get(x, y) == true ? Color.BLACK : Color.WHITE); } } } catch (WriterException e) { e.printStackTrace(); } return bmp; }
Example #18
Source File: ReceiveFragment.java From xmrwallet with Apache License 2.0 | 6 votes |
public Bitmap generate(String text, int width, int height) { if ((width <= 0) || (height <= 0)) return null; Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); try { BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); int[] pixels = new int[width * height]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (bitMatrix.get(j, i)) { pixels[i * width + j] = 0x00000000; } else { pixels[i * height + j] = 0xffffffff; } } } Bitmap bitmap = Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565); bitmap = addLogo(bitmap); return bitmap; } catch (WriterException ex) { Timber.e(ex); } return null; }
Example #19
Source File: GroupDetailActivity.java From Rumble with GNU General Public License v3.0 | 5 votes |
public void invite() { String buffer = group.getGroupBase64ID(); int size = 200; Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); QRCodeWriter qrCodeWriter = new QRCodeWriter(); try { BitMatrix bitMatrix = qrCodeWriter.encode(buffer, BarcodeFormat.QR_CODE, size, size, hintMap); Bitmap image = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); if(image != null) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { image.setPixel(i, j, bitMatrix.get(i, j) ? Color.BLACK : Color.WHITE); } } Intent intent = new Intent(this, DisplayQRCode.class); intent.putExtra("EXTRA_GROUP_NAME", groupName); intent.putExtra("EXTRA_BUFFER", buffer); intent.putExtra("EXTRA_QRCODE", image); startActivity(intent); overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_open_exit); } }catch(WriterException ignore) { } //} }
Example #20
Source File: ImageMapRenderer.java From MCAuthenticator with GNU General Public License v3.0 | 5 votes |
private BitMatrix getQRMap(String username, String secret, String serverIp) throws WriterException { return new QRCodeWriter().encode(String.format(TOTP_URL_FORMAT, username, serverIp, secret), BarcodeFormat.QR_CODE, 128, 128); }
Example #21
Source File: QrPanel.java From raccoon4 with Apache License 2.0 | 5 votes |
/** * Create a new panel of a given size. * * @param size * panel width/height */ public QrPanel(int size) { this.size = size; qrCodeWriter = new QRCodeWriter(); hintMap = new HashMap<EncodeHintType, Object>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); image = BLANK; }
Example #22
Source File: EncodingUtils.java From AirFree-Client with GNU General Public License v3.0 | 5 votes |
/** * 创建二维码 * * @param content content * @param widthPix widthPix * @param heightPix heightPix * @param logoBm logoBm * @return 二维码 */ public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) { try { if (content == null || "".equals(content)) { return null; } // 配置参数 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 容错级别 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 图像数据转换,使用了矩阵转换 BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints); int[] pixels = new int[widthPix * heightPix]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < heightPix; y++) { for (int x = 0; x < widthPix; x++) { if (bitMatrix.get(x, y)) { pixels[y * widthPix + x] = 0xff000000; } else { pixels[y * widthPix + x] = 0xffffffff; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix); if (logoBm != null) { bitmap = addLogo(bitmap, logoBm); } //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大! return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; }
Example #23
Source File: QrCodeHelper.java From FamilyChat with Apache License 2.0 | 5 votes |
/** * 创建二维码 * * @param content content 待转换的字符串 * @param widthPix widthPix 输出结果bmp的宽(px) * @param heightPix heightPix 输出结果bmp的高(px) * @param logoBm logoBm 中间的logo(默认大小为最终二维码大小的1/5) * @return 二维码bmp */ public static Bitmap createQrCode(String content, int widthPix, int heightPix, Bitmap logoBm) { try { if (content == null || content.length() == 0) return null; // 配置参数 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 容错级别 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 图像数据转换,使用了矩阵转换 BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints); int[] pixels = new int[widthPix * heightPix]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < heightPix; y++) { for (int x = 0; x < widthPix; x++) { if (bitMatrix.get(x, y)) pixels[y * widthPix + x] = 0xff000000; else pixels[y * widthPix + x] = 0xffffffff; } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix); if (logoBm != null) bitmap = addLogo(bitmap, logoBm); return bitmap; } catch (WriterException e) { Log.e("QrCodeHelper", "create qrcode error:" + e.toString()); } return null; }
Example #24
Source File: EncodingUtils.java From myapplication with Apache License 2.0 | 5 votes |
/** * 创建二维码 * * @param content content * @param widthPix widthPix * @param heightPix heightPix * @param logoBm logoBitmap * @param foreColor 前景色 * @param backColor 背景色 * @return 二维码 */ public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm, int foreColor, int backColor) { try { if (content == null || "".equals(content)) { return null; } // 配置参数 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 容错级别 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 图像数据转换,使用了矩阵转换 BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints); int[] pixels = new int[widthPix * heightPix]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < heightPix; y++) { for (int x = 0; x < widthPix; x++) { if (bitMatrix.get(x, y)) { pixels[y * widthPix + x] = foreColor; } else { pixels[y * widthPix + x] = backColor; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix); if (logoBm != null) { bitmap = addLogo(bitmap, logoBm); } //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大! return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; }
Example #25
Source File: QRCodeUtil.java From talk-android with MIT License | 5 votes |
public static Bitmap encode(String str, int size) { try { // 判断URL合法性 if (str == null || "".equals(str) || str.length() < 1) { return null; } Hashtable<EncodeHintType, String> hints = new Hashtable<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 图像数据转换,使用了矩阵转换 BitMatrix bitMatrix = new QRCodeWriter().encode(str, BarcodeFormat.QR_CODE, size, size, hints); int[] pixels = new int[size * size]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (bitMatrix.get(x, y)) { pixels[y * size + x] = 0xff000000; } else { pixels[y * size + x] = 0xffffffff; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, size, 0, 0, size, size); return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; }
Example #26
Source File: MCRQRCodeServlet.java From mycore with GNU General Public License v3.0 | 5 votes |
private MCRContent getPNGContent(final String url, final int size) throws IOException { QRCodeWriter writer = new QRCodeWriter(); BitMatrix matrix; try { matrix = writer.encode(url, BarcodeFormat.QR_CODE, size, size); } catch (WriterException e) { throw new IOException(e); } BufferedImage image = toBufferedImage(matrix); return pngTools.toPNGContent(image); }
Example #27
Source File: TransferEntriesActivity.java From Aegis with GNU General Public License v3.0 | 5 votes |
private void generateQR() { GoogleAuthInfo selectedEntry = _authInfos.get(_currentEntryCount - 1); _issuer.setText(selectedEntry.getIssuer()); _accountName.setText(selectedEntry.getAccountName()); _entriesCount.setText(String.format(getString(R.string.entries_count), _currentEntryCount, _authInfos.size())); QRCodeWriter writer = new QRCodeWriter(); BitMatrix bitMatrix = null; try { bitMatrix = writer.encode(selectedEntry.getUri().toString(), BarcodeFormat.QR_CODE, 512, 512); } catch (WriterException e) { Dialogs.showErrorDialog(this, R.string.unable_to_generate_qrcode, e); return; } int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); _qrImage.setImageBitmap(bitmap); }
Example #28
Source File: BarcodeImage.java From nordpos with GNU General Public License v3.0 | 5 votes |
public static Image getQRCode(String value) { BitMatrix matrix; com.google.zxing.Writer writer = new QRCodeWriter(); try { matrix = writer.encode(value, com.google.zxing.BarcodeFormat.QR_CODE, 100, 100); return (Image) MatrixToImageWriter.toBufferedImage(matrix); } catch (WriterException ex) { return null; } }
Example #29
Source File: BluetoothServer.java From ShootOFF with GNU General Public License v3.0 | 5 votes |
private Optional<Image> generateQrCode(String address) { final QRCodeWriter qrCodeWriter = new QRCodeWriter(); final int width = 300; final int height = 300; try { final BitMatrix byteMatrix = qrCodeWriter.encode(address, BarcodeFormat.QR_CODE, width, height); final BufferedImage qrCodeImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); qrCodeImage.createGraphics(); final Graphics2D graphics = (Graphics2D) qrCodeImage.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, width, height); graphics.setColor(Color.BLACK); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (byteMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } return Optional.of(SwingFXUtils.toFXImage(qrCodeImage, null)); } catch (WriterException e) { logger.error("Failed to encode local bluetooth address as a qr code", e); return Optional.empty(); } }
Example #30
Source File: MainActivity.java From SimpleDialogFragments with Apache License 2.0 | 5 votes |
/** * Create and return your Image here. * NOTE: make sure, your class is public and has a public default constructor! * * @param tag The dialog-fragments tag * @param extras The extras supplied to {@link SimpleImageDialog#extra(Bundle)} * @return the image to be shown */ @Override public Bitmap create(@Nullable String tag, @NonNull Bundle extras) { String content = extras.getString(QR_CONTENT); if (content == null) return null; // Generate try { EnumMap<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q); hints.put(EncodeHintType.CHARACTER_SET, "UTF8"); BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, 1024, 1024, hints); int width = bitMatrix.getWidth(), height = bitMatrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { pixels[y*width + x] = bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE; } } Bitmap qr = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); qr.setPixels(pixels, 0, width, 0, 0, width, height); return qr; } catch (WriterException ignored) {} return null; }