Java 类com.google.zxing.qrcode.QRCodeWriter 实例源码

项目:mDL-ILP    文件:QRUtils.java   
public static Bitmap getQR(final byte[] content, final int size) {
    final QRCodeWriter writer = new QRCodeWriter();
    try {
        final Map<EncodeHintType, Object> encodingHints = new HashMap<>();
        encodingHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        encodingHints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

        final BitMatrix encoding = writer.encode(Utils.makeQR(content), BarcodeFormat.QR_CODE, size, size, encodingHints);
        final Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                bitmap.setPixel(i, j, encoding.get(i, j) ? COLOR_DARK : COLOR_LIGHT);
            }
        }

        return bitmap;
    } catch (WriterException e) {
        Log.e("QRUtils", "Failed to get QR code", e);
    }
    return null;
}
项目:androidtools    文件:QRCodeUtils.java   
/**
 * Create QR code
 *
 * @param content                  content
 * @param widthPix                 widthPix
 * @param heightPix                heightPix
 * @param openErrorCorrectionLevel Does fault tolerance open?
 * @param logoBitmap               The two-dimensional code Logo icon (Center for null)
 * @param filePath                 The file path used to store two-dimensional code images
 * @return Generate two-dimensional code and save the file is successful [boolean]
 */
public static boolean createQRCode(String content, int widthPix, int heightPix, boolean openErrorCorrectionLevel, Bitmap logoBitmap, String filePath) {
    try {
        if (TextUtils.isEmpty(content) || TextUtils.equals("null", content) || "".equals(content)) {
            return false;
        }
        Map hints = openErrorCorrectionLevel(openErrorCorrectionLevel);
        // Image data conversion, the use of matrix conversion
        BitMatrix bitMatrix = new QRCodeWriter().encode(new String(content.getBytes("UTF-8"), "iso-8859-1"), BarcodeFormat.QR_CODE, widthPix, heightPix, hints);

        Bitmap bitmap = generateQRBitmap(bitMatrix);

        if (logoBitmap != null) {
            bitmap = addLogo(bitmap, logoBitmap);
        }
        boolean compress = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));
        //You must use the compress method to save the bitmap to the file and then read it.
        //The bitmap returned directly is without any compression, and the memory consumption is huge!
        return bitmap != null && bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));
    } catch (WriterException | IOException e) {
        e.printStackTrace();
    }

    return false;
}
项目:PeSanKita-android    文件:QrCode.java   
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);
  }
}
项目:Nird2    文件:QrCodeUtils.java   
@Nullable
static Bitmap createQrCode(DisplayMetrics dm, String input) {
    int smallestDimen = Math.min(dm.widthPixels, dm.heightPixels);
    try {
        // Generate QR code
        final BitMatrix encoded = new QRCodeWriter().encode(
                input, QR_CODE, smallestDimen, smallestDimen);
        // Convert QR code to Bitmap
        int width = encoded.getWidth();
        int height = encoded.getHeight();
        int[] pixels = new int[width * height];
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                pixels[y * width + x] = encoded.get(x, y) ? BLACK : WHITE;
            }
        }
        Bitmap qr = Bitmap.createBitmap(width, height, ARGB_8888);
        qr.setPixels(pixels, 0, width, 0, 0, width, height);
        return qr;
    } catch (WriterException e) {
        if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
        return null;
    }
}
项目:Nird2    文件:QrCodeUtils.java   
@Nullable
static Bitmap createQrCode(DisplayMetrics dm, String input) {
    int smallestDimen = Math.min(dm.widthPixels, dm.heightPixels);
    try {
        // Generate QR code
        final BitMatrix encoded = new QRCodeWriter().encode(
                input, QR_CODE, smallestDimen, smallestDimen);
        // Convert QR code to Bitmap
        int width = encoded.getWidth();
        int height = encoded.getHeight();
        int[] pixels = new int[width * height];
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                pixels[y * width + x] = encoded.get(x, y) ? BLACK : WHITE;
            }
        }
        Bitmap qr = Bitmap.createBitmap(width, height, ARGB_8888);
        qr.setPixels(pixels, 0, width, 0, 0, width, height);
        return qr;
    } catch (WriterException e) {
        if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
        return null;
    }
}
项目:Protestr    文件:ImageUtils.java   
public static void generateQr(final OnQrGeneratedListener onQrGeneratedListener, final String seed) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            final QRCodeWriter writer = new QRCodeWriter();
            try {
                BitMatrix bitMatrix = writer.encode(seed, BarcodeFormat.QR_CODE, 512, 512);
                int width = bitMatrix.getWidth();
                int height = bitMatrix.getHeight();
                Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
                for (int x = 0; x < width; x++) {
                    for (int y = 0; y < height; y++) {
                        bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
                    }
                }
                onQrGeneratedListener.onQrGenerated(bitmap);
            } catch (WriterException e) {
                e.printStackTrace();
            }
        }
    }).start();
}
项目:Easy-WeChat    文件:QRCodeUtil.java   
/**
 * 关于Ansi Color的信息,参考:http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
 *
 * @param text
 */
public static void printQRCodeInTerminal(String text) {
    int qrcodeWidth = 400;
    int qrcodeHeight = 400;
    HashMap<EncodeHintType, String> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    try {
        BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, qrcodeWidth, qrcodeHeight, hints);

        // 将像素点转成格子
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();

        for (int y = 30; y < height - 30; y += 10) {
            for (int x = 30; x < width - 20; x += 10) {
                boolean isBlack = bitMatrix.get(x, y);
                System.out.print(isBlack ? "  " : "\u001b[47m  \u001b[0m");
            }
            System.out.println();
        }
    }
    catch (WriterException e) {
        e.printStackTrace();
    }
}
项目:xmrwallet    文件:ReceiveFragment.java   
public Bitmap generate(String text, int width, int height) {
    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 e) {
        e.printStackTrace();
    }
    return null;
}
项目:phonk    文件:PMedia.java   
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;
}
项目:Cable-Android    文件:QrCode.java   
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);
  }
}
项目:WiFiKeyShare    文件:QrCodeUtils.java   
/**
 * 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;
}
项目:TenguChat    文件:BarcodeProvider.java   
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);
        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;
    }
}
项目:messengerxmpp    文件:XmppActivity.java   
protected Bitmap createQrCodeBitmap(String input, int size) {
    Log.d(Config.LOGTAG,"qr code requested size: "+size);
    try {
        final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
        final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        final BitMatrix result = QR_CODE_WRITER.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.TRANSPARENT;
            }
        }
        final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Log.d(Config.LOGTAG,"output size: "+width+"x"+height);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    } catch (final WriterException e) {
        return null;
    }
}
项目:Metro    文件:QRCodeUtil.java   
/**
 * 生成二维码
 *
 * @param text 需要生成二维码的文字、网址等
 * @param size 需要生成二维码的大小()
 * @return bitmap
 */
public static Bitmap createQRCode(String text, int size) {
    try {
        Hashtable<EncodeHintType, String> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new QRCodeWriter().encode(text,
                BarcodeFormat.QR_CODE, size, size, hints);
        int[] pixels = new int[size * size];
        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;
                }
            }
        }
        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;
    }
}
项目:Biu    文件:ConnectingActivity.java   
/**
 * 生成二维码
 *
 * @param url 二维码包含的URL信息
 * @return Bitmap
 */
@SuppressWarnings("SuspiciousNameCombination")
private Bitmap genQRCode(String url) {

    // 等待placeholder绘制完成之后调用
    int width = mQRCodeImage.getMeasuredWidth();

    Bitmap bitmap = null;
    QRCodeWriter writer = new QRCodeWriter();
    try {
        BitMatrix bm = writer.encode(url, BarcodeFormat.QR_CODE, width, width);
        bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < width; j++) {
                bitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE);
            }
        }
    } catch (WriterException e) {
        Log.w(TAG, e.toString());
    }
    return bitmap;
}
项目:toshi-android-client    文件:ImageUtil.java   
private static Bitmap generateQrCodeBitmap(@NonNull final String value) throws WriterException {
    final QRCodeWriter writer = new QRCodeWriter();
    final int size = BaseApplication.get().getResources().getDimensionPixelSize(R.dimen.qr_code_size);
    final Map<EncodeHintType, Integer> map = new HashMap<>();
    map.put(EncodeHintType.MARGIN, 0);
    final BitMatrix bitMatrix = writer.encode(value, BarcodeFormat.QR_CODE, size, size, map);
    final int width = bitMatrix.getWidth();
    final int height = bitMatrix.getHeight();
    final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    final int contrastColour = ContextCompat.getColor(BaseApplication.get(), R.color.windowBackground);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : contrastColour);
        }
    }
    return bmp;
}
项目:ExamplesAndroid    文件:MainActivity.java   
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;
}
项目:live-event-android    文件:ShareHandleActivity.java   
private void setupQrcode() {
    QRCodeWriter writer = new QRCodeWriter();
    try {
        BitMatrix bitMatrix = writer.encode(profile, BarcodeFormat.QR_CODE, 512, 512);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        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, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
            }
        }
        ((ImageView) findViewById(R.id.qrcode)).setImageBitmap(bmp);

    } catch (WriterException e) {
        e.printStackTrace();
    }
}
项目:PROJECT_QR    文件:QR.java   
/**
 * Creates QRCode image of size 400X400
 * @param dataPath Path to input data
 * @throws Exception
 */
public static void generateQRCode(String dataPath) throws Exception
{       
        String data =read_from_file(dataPath);
        QRCodeWriter writer = new QRCodeWriter();
        String genqr=QRCode.filePath+"/QRCode.png";
        int img_size=400;       
        BitMatrix bm = writer.encode(data, BarcodeFormat.QR_CODE,img_size,img_size);
        Bitmap bmp = Bitmap.createBitmap(img_size,img_size,Bitmap.Config.ARGB_8888);        
        if (bmp != null) 
        {
            File f=new File(genqr);
            if(f.exists())
                f.delete();
            FileOutputStream gqr=new FileOutputStream(genqr);
            for (int i = 0; i < img_size; i++)         
                for (int j = 0; j < img_size; j++) 
                    bmp.setPixel(i, j, bm.get(i, j) ? Color.BLACK: Color.WHITE);        
            bmp.compress(Bitmap.CompressFormat.PNG, 100,gqr);
            str+="\nQRCode img: "+genqr;                        
            gqr.close();
        }
        else
            throw new WriterException("QRCode generation failed!");

}
项目:youscope    文件:ManageAddDeviceFrame.java   
private void setDriver(String libraryID)
{
    if(libraryID == null)
    {
        this.image = null;
        repaint();
        return;
    }
    BitMatrix matrix;
    try
    {
        matrix = new QRCodeWriter().encode(QR_MESSAGE_PREFIX + libraryID, com.google.zxing.BarcodeFormat.QR_CODE, WIDTH, HEIGHT);
    }
    catch(@SuppressWarnings("unused") WriterException e)
    {
        this.image = null;
        repaint();
        return;
    }

    this.image = MatrixToImageWriter.toBufferedImage(matrix);
    repaint();
    return;
}
项目:AluShare    文件:QRCodeGenerator.java   
public Bitmap generateQRCode() {
    QRCodeWriter writer = new QRCodeWriter();
    try {

        BitMatrix matrix = writer.encode(this.content, BarcodeFormat.QR_CODE, 512, 512);
        Bitmap bitmap = Bitmap.createBitmap(matrix.getWidth(), matrix.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas tempCanvas = new Canvas(bitmap);
        tempCanvas.drawColor(0, PorterDuff.Mode.CLEAR);

        for (int x = 0; x < matrix.getWidth(); x++) {
            for (int y = 0; y < matrix.getHeight(); y++) {
                bitmap.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.TRANSPARENT);

            }
        }

        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
项目:FileCast    文件:QrView.java   
public void setContentString(String content) throws WriterException {
    Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, getWidth(),
            getWidth(), hintMap);
    int size = byteMatrix.getWidth();

    image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setColor(getBackground());
    graphics.fillRect(0, 0, size, size);
    graphics.setColor(Color.BLACK);

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (byteMatrix.get(i, j)) {
                graphics.fillRect(i, j, 1, 1);
            }
        }
    }
    repaint();
}
项目:Pix-Art-Messenger    文件:BarcodeProvider.java   
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);
        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;
    }
}
项目:eds-starter6-jpa    文件:QRCodeController.java   
@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();
    }
}
项目:eds-starter6-mongodb    文件:QRCodeController.java   
@RequireAnyAuthority
@RequestMapping(value = "/qr", method = RequestMethod.GET)
public void qrcode(HttpServletResponse response,
        @AuthenticationPrincipal MongoUserDetails userDetails)
        throws WriterException, IOException {

    User user = userDetails.getUser(this.mongoDb);
    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();
    }
}
项目:TimberdoodleApp    文件:QRReaderWriter.java   
/**
 * Creates a QR code bitmap with the specified dimensions from the specified string.
 *
 * @param content The data the QR code should represent.
 * @param width   The preferred width of the bitmap.
 * @param height  The preferred height of the bitmap.
 * @return The bitmap containing the QR code.
 */
protected Bitmap createQRCode(String content, int width, int height) {
    // Create BitMatrix from content string
    BitMatrix bitMatrix;
    QRCodeWriter writer = new QRCodeWriter();
    try {
        bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height);
    } catch (WriterException e) {
        throw new RuntimeException(e);
    }

    // Create bitmap from BitMatrix
    width = bitMatrix.getWidth();
    height = bitMatrix.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
        }
    }

    return bitmap;
}
项目:Bridgewalker-Android    文件:QRCodeUtils.java   
public static Bitmap encodeAsBitmap(String contents, int size) {
    try {
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix result = writer.encode(contents, BarcodeFormat.QR_CODE, size, size);

        int width = result.getWidth();
        int height = result.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] = result.get(x, y) ? BLACK : WHITE;
          }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    } catch (WriterException e) {
        throw new RuntimeException(e);
    }
}
项目:Conversations    文件:BarcodeProvider.java   
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);
        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;
    }
}
项目:sturesy-android-client    文件:QRCodeGenerator.java   
/**
 * Generates a QR-Code-Image from given String
 * 
 * @param url
 * @param width
 * @param height
 * @throws WriterException
 */
public static Bitmap getQRImage(String url, int size) {
    Bitmap mBitmap;
    BitMatrix bitMatrix;
    @SuppressWarnings("unused")
    QRCode qr = new QRCode();
    QRCodeWriter writer = new QRCodeWriter();
    mBitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888);
    try {
        bitMatrix = writer.encode(url, BarcodeFormat.QR_CODE, size, size);
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                mBitmap.setPixel(i, j, bitMatrix.get(i, j) ? Color.BLACK
                        : Color.WHITE);
            }
        }
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return mBitmap;
}
项目:drone-slam    文件:QRCodeGeneratorPanel.java   
private void generateQRCode(String contents) {
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = writer.encode(contents, BarcodeFormat.QR_CODE, Integer.parseInt(width.getText()), Integer.parseInt(height.getText()));
            BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);

            setImage(image);
        } catch (WriterException e) {
            e.printStackTrace();
        }

//      setSize(new Dimension(Math.min(300, Integer.parseInt(width.getText())), Math.min(400, Integer.parseInt(height.getText()))));
//      setPreferredSize(new Dimension(Math.min(300, Integer.parseInt(width.getText())), Math.min(400, Integer.parseInt(height.getText()))));
//      setMinimumSize(new Dimension(Math.min(300, Integer.parseInt(width.getText())), Math.min(400, Integer.parseInt(height.getText()))));
//      setMaximumSize(new Dimension(Math.min(300, Integer.parseInt(width.getText())), Math.min(400, Integer.parseInt(height.getText()))));
    }
项目:wechat-api    文件:QRCodeUtils.java   
/**
 * 显示二维码,Linux会显示在终端
 *
 * @param qrCode
 * @param terminal
 */
public static void showQrCode(File qrCode, boolean terminal) throws WriterException {
    if (!terminal) {
        String os = System.getProperty("os.name").toLowerCase();
        try {
            if (os.contains("mac") || os.contains("win")) {
                Desktop.getDesktop().open(qrCode);
                return;
            }
        } catch (Exception e) {
            log.warn("在 {} 下打开文件 {} 失败", os, qrCode.getPath(), e);
        }
    }
    Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
    hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    // Now with zxing version 3.2.1 you could change border size (white border size to just 1)
    // default = 4
    hintMap.put(EncodeHintType.MARGIN, 1);
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

    String       qrContent    = QRCodeUtils.readQRCode(qrCode, hintMap);
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix    bitMatrix;
    bitMatrix = qrCodeWriter.encode(qrContent, BarcodeFormat.QR_CODE, 10, 10, hintMap);
    System.out.println(toAscii(bitMatrix));
}
项目:bkbc-bitcoinpos    文件:QrCodeEncoder.java   
public int[] getPixels(String content, int dimention)
        throws WriterException {

    QRCodeWriter writer = new QRCodeWriter();

    EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(
            EncodeHintType.class);
    hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE,
            dimention, dimention, hint);
    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) ? 0xFF000000
                    : 0xFFFFFFFF;
            // pixels[offset + x] = bitMatrix.get(x, y) ? colorBack :
            // colorFront;
        }
    }

    return pixels;

}
项目:springsecuritytotp    文件:QRCodeController.java   
@RequestMapping(value = "/qrcode/{username}.png", method = RequestMethod.GET)
public void qrcode(HttpServletResponse response,
        @PathVariable("username") String username)
        throws WriterException, IOException {

    User user = this.userRepository.findByUserName(username);
    if (user != null) {
        response.setContentType("image/png");
        String contents = "otpauth://totp/" + username + ":" + user.getEmail()
                + "?secret=" + user.getSecret() + "&issuer=SpringSecurityTOTP";

        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix matrix = writer.encode(contents, BarcodeFormat.QR_CODE, 200, 200);
        MatrixToImageWriter.writeToStream(matrix, "PNG", response.getOutputStream());
        response.getOutputStream().flush();
    }
}
项目:nordpos    文件:PrintItemBarcode.java   
@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);
}
项目:WearBusinessCard    文件:Tools.java   
public static Bitmap makeQRCode(String content){
    QRCodeWriter writer = new QRCodeWriter();
    Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    hints.put(EncodeHintType.MARGIN, 2);
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    try {
        BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 320, 320, hints);
        Bitmap bm = toBitmap(matrix);
        bm = Bitmap.createBitmap(bm, 10, 10, bm.getWidth() - 20, bm.getHeight() - 20);
        return bm;
        //saveImage(bm);
    } catch (WriterException e1) {
        e1.printStackTrace();
    }
    return null;
}
项目:frozenchat    文件:XmppActivity.java   
protected Bitmap createQrCodeBitmap(String input, int size) {
    Log.d(Config.LOGTAG,"qr code requested size: "+size);
    try {
        final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
        final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        final BitMatrix result = QR_CODE_WRITER.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.TRANSPARENT;
            }
        }
        final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Log.d(Config.LOGTAG,"output size: "+width+"x"+height);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    } catch (final WriterException e) {
        return null;
    }
}
项目:Eye-Fi    文件:EyeFi.java   
public BufferedImage makeQRCode(byte[] data) {

        try {
            Hashtable<EncodeHintType, ErrorCorrectionLevel> hints = new Hashtable<>();
            hints.put(EncodeHintType.ERROR_CORRECTION, (errorCorrectionLevel == 0) ? ErrorCorrectionLevel.L
                    : (errorCorrectionLevel == 1) ? ErrorCorrectionLevel.M
                            : (errorCorrectionLevel == 2) ? ErrorCorrectionLevel.Q
                                    : (errorCorrectionLevel == 3) ? ErrorCorrectionLevel.H
                                            : null);
            BitMatrix matrix = new QRCodeWriter().encode(new String(data, Charset.forName("ISO-8859-1")),
                    com.google.zxing.BarcodeFormat.QR_CODE, QRSize, QRSize, hints);
            dataSendLength = data.length;
            qrCode = MatrixToImageWriter.toBufferedImage(matrix);
            return qrCode;
        } catch (WriterException ex) {
            ex.printStackTrace();
            return null;
        }
    }
项目:txtr    文件:XmppActivity.java   
protected Bitmap createQrCodeBitmap(String input, int size) {
    Log.d(Config.LOGTAG,"qr code requested size: "+size);
    try {
        final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
        final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        final BitMatrix result = QR_CODE_WRITER.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.TRANSPARENT;
            }
        }
        final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Log.d(Config.LOGTAG,"output size: "+width+"x"+height);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    } catch (final WriterException e) {
        return null;
    }
}
项目:yasme-android    文件:QR.java   
public Bitmap generateQRCode(String data) {
    Log.d(getClass().getName(), "Generate QR for " + data);
    com.google.zxing.Writer writer = new QRCodeWriter();
    BitMatrix bm;
    try {
        bm = writer.encode(data, BarcodeFormat.QR_CODE,SIZE, SIZE);
    } catch (Exception e) {
        return null;
    }

    Bitmap imageBitmap = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);

    for (int i = 0; i < SIZE; i++) {//width
        for (int j = 0; j < SIZE; j++) {//height
            imageBitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK: Color.WHITE);
        }
    }
    return imageBitmap;
}
项目:sqrl-shiro    文件:QRCode.java   
public static BufferedImage createQRCode(byte[] byteData, int width, int height) {
    char[] data = new char[byteData.length];
    for(int i = data.length-1; i >= 0; i--) {
        data[i] = (char)(0xFF & byteData[i]); // flip signed to unsigned
    }

    QRCodeWriter writer = new QRCodeWriter();
    try {
        // default encoding is full-width bytes
        BitMatrix bitMatrix = writer.encode(new String(data), BarcodeFormat.QR_CODE, width, height);
        BufferedImage image = toBufferedImage(bitMatrix);
        return image;
    } catch( WriterException e ) {
        e.printStackTrace(); // TODO re-throw
    }
    return null;
}