Search in sources :

Example 1 with MultiFormatWriter

use of com.google.zxing.MultiFormatWriter in project android-zxingLibrary by yipianfengye.

the class EncodingHandler method createQRCode.

public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = BLACK;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) EncodeHintType(com.google.zxing.EncodeHintType) MultiFormatWriter(com.google.zxing.MultiFormatWriter) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix)

Example 2 with MultiFormatWriter

use of com.google.zxing.MultiFormatWriter in project weex-example by KalicyZhou.

the class QRCodeEncoder method encodeAsBitmap.

Bitmap encodeAsBitmap() throws WriterException {
    String contentsToEncode = contents;
    if (contentsToEncode == null) {
        return null;
    }
    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contentsToEncode);
    if (encoding != null) {
        hints = new EnumMap<>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    BitMatrix result;
    try {
        result = new MultiFormatWriter().encode(contentsToEncode, format, dimension, dimension, hints);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    }
    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;
}
Also used : Bitmap(android.graphics.Bitmap) EncodeHintType(com.google.zxing.EncodeHintType) MultiFormatWriter(com.google.zxing.MultiFormatWriter) BitMatrix(com.google.zxing.common.BitMatrix)

Example 3 with MultiFormatWriter

use of com.google.zxing.MultiFormatWriter in project zxing by zxing.

the class CommandLineEncoder method main.

public static void main(String[] args) throws Exception {
    EncoderConfig config = new EncoderConfig();
    JCommander jCommander = new JCommander(config, args);
    jCommander.setProgramName(CommandLineEncoder.class.getSimpleName());
    if (config.help) {
        jCommander.usage();
        return;
    }
    String outFileString = config.outputFileBase;
    if (EncoderConfig.DEFAULT_OUTPUT_FILE_BASE.equals(outFileString)) {
        outFileString += '.' + config.imageFormat.toLowerCase(Locale.ENGLISH);
    }
    Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
    if (config.errorCorrectionLevel != null) {
        hints.put(EncodeHintType.ERROR_CORRECTION, config.errorCorrectionLevel);
    }
    BitMatrix matrix = new MultiFormatWriter().encode(config.contents.get(0), config.barcodeFormat, config.width, config.height, hints);
    MatrixToImageWriter.writeToPath(matrix, config.imageFormat, Paths.get(outFileString));
}
Also used : EncodeHintType(com.google.zxing.EncodeHintType) MultiFormatWriter(com.google.zxing.MultiFormatWriter) JCommander(com.beust.jcommander.JCommander) BitMatrix(com.google.zxing.common.BitMatrix) EnumMap(java.util.EnumMap)

Example 4 with MultiFormatWriter

use of com.google.zxing.MultiFormatWriter in project QRCode by 5peak2me.

the class EncodingHandler method createQRCode.

public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = BLACK;
            } else {
                //解决此Bitmap保存到本地图片变黑色的bug
                pixels[y * width + x] = BACKGROUNDCOLRO;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) EncodeHintType(com.google.zxing.EncodeHintType) MultiFormatWriter(com.google.zxing.MultiFormatWriter) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix)

Example 5 with MultiFormatWriter

use of com.google.zxing.MultiFormatWriter in project BGAQRCode-Android by bingoogolapple.

the class QRCodeEncoder method syncEncodeQRCode.

/**
     * 同步创建指定前景色、指定背景色、带logo的二维码图片。该方法是耗时操作,请在子线程中调用。
     *
     * @param content         要生成的二维码图片内容
     * @param size            图片宽高,单位为px
     * @param foregroundColor 二维码图片的前景色
     * @param backgroundColor 二维码图片的背景色
     * @param logo            二维码图片的logo
     */
public static Bitmap syncEncodeQRCode(String content, int size, int foregroundColor, int backgroundColor, Bitmap logo) {
    try {
        BitMatrix matrix = new MultiFormatWriter().encode(content, 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 (matrix.get(x, y)) {
                    pixels[y * size + x] = foregroundColor;
                } else {
                    pixels[y * size + x] = backgroundColor;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
        return addLogoToQRCode(bitmap, logo);
    } catch (Exception e) {
        return null;
    }
}
Also used : Bitmap(android.graphics.Bitmap) MultiFormatWriter(com.google.zxing.MultiFormatWriter) BitMatrix(com.google.zxing.common.BitMatrix)

Aggregations

MultiFormatWriter (com.google.zxing.MultiFormatWriter)9 BitMatrix (com.google.zxing.common.BitMatrix)9 Bitmap (android.graphics.Bitmap)7 EncodeHintType (com.google.zxing.EncodeHintType)7 Hashtable (java.util.Hashtable)3 JCommander (com.beust.jcommander.JCommander)1 EnumMap (java.util.EnumMap)1