Java Code Examples for com.google.zxing.MultiFormatWriter#encode()

The following examples show how to use com.google.zxing.MultiFormatWriter#encode() . 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: QRCodeGenerator.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 9 votes vote down vote up
public static Bitmap transform(byte[] inputData) {
    String encoded;
    encoded = Base64.encodeToString(inputData, Base64.DEFAULT);

    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    Bitmap bitmap = null;
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(encoded, BarcodeFormat.QR_CODE, 1000, 1000);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        bitmap = barcodeEncoder.createBitmap(bitMatrix);
    } catch (WriterException e) {
        Timber.e(e);
    }

    return bitmap;
}
 
Example 2
Source File: HistoryDetailsActivity.java    From SecScanQR with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method creates a picture of the scanned qr code
 */
private void showQrImage() {
    multiFormatWriter = new MultiFormatWriter();
    Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);

    hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    try{
        BarcodeFormat format = generalHandler.StringToBarcodeFormat(selectedFormat);
        BitMatrix bitMatrix = multiFormatWriter.encode(selectedCode, format, 250,250, hintMap);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        bitmap = barcodeEncoder.createBitmap(bitMatrix);
        codeImage.setImageBitmap(bitmap);
    } catch (Exception e){
        codeImage.setVisibility(View.GONE);
    }
}
 
Example 3
Source File: BarcodeUtils.java    From code-scanner with MIT License 6 votes vote down vote up
/**
 * Encode text content
 *
 * @param content Text to be encoded
 * @param format  Result barcode format
 * @param width   Result image width
 * @param height  Result image height
 * @param hints   Encoder hints
 * @return Barcode bit matrix, if it was encoded successfully, {@code null} otherwise
 * @see EncodeHintType
 * @see BitMatrix
 */
@Nullable
public static BitMatrix encodeBitMatrix(@NonNull final String content,
        @NonNull final BarcodeFormat format, final int width, final int height,
        @Nullable final Map<EncodeHintType, ?> hints) {
    Objects.requireNonNull(content);
    Objects.requireNonNull(format);
    final MultiFormatWriter writer = new MultiFormatWriter();
    try {
        if (hints != null) {
            return writer.encode(content, format, width, height, hints);
        } else {
            return writer.encode(content, format, width, height);
        }
    } catch (final WriterException e) {
        return null;
    }
}
 
Example 4
Source File: QREncoder.java    From Lunary-Ethereum-Wallet with GNU General Public License v3.0 6 votes vote down vote up
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    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;
}
 
Example 5
Source File: ZxingUtils.java    From wish-pay with Apache License 2.0 6 votes vote down vote up
/**
 * 二维码信息写成JPG BufferedImage
 *
 * @param content
 * @param width
 * @param height
 * @return
 */
public static BufferedImage writeInfoToJpgBuffImg(String content, int width, int height) {
    if (width < 250) {
        width = 250;
    }
    if (height < 250) {
        height = 250;
    }
    BufferedImage re = null;

    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    Map hints = new HashMap();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(content,
                BarcodeFormat.QR_CODE, width, height, hints);
        re = MatrixToImageWriter.toBufferedImage(bitMatrix);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return re;
}
 
Example 6
Source File: QRShowActivity.java    From trigger with GNU General Public License v2.0 6 votes vote down vote up
private void generateQR(Setup setup) throws Exception {
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    int data_length = 0;
    try {
        JSONObject obj = Settings.toJsonObject(setup);
        String data = encodeSetup(obj);
        data_length = data.length();

        // data has to be a string
        BitMatrix bitMatrix = multiFormatWriter.encode(data, BarcodeFormat.QR_CODE, 1080, 1080);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
        ((ImageView) findViewById(R.id.QRView)).setImageBitmap(bitmap);
    } catch (WriterException e) {
        Toast.makeText(this, e.getMessage() + " (" + data_length + " Bytes)", Toast.LENGTH_LONG).show();
        finish();
    }
}
 
Example 7
Source File: QrCodeUtils.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
public static Bitmap textToBarCode(String data) {
    MultiFormatWriter writer = new MultiFormatWriter();


    String finaldata = Uri.encode(data, "utf-8");

    BitMatrix bm = null;
    try {
        bm = writer.encode(finaldata, BarcodeFormat.CODE_128, 150, 150);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = Bitmap.createBitmap(180, 40, Bitmap.Config.ARGB_8888);

    for (int i = 0; i < 180; i++) {//width
        for (int j = 0; j < 40; j++) {//height
            bitmap.setPixel(i, j, bm.get(i, j) ? BLACK : WHITE);
        }
    }

    return bitmap;
}
 
Example 8
Source File: ScannerActivity.java    From SecScanQR with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method creates a picture of the scanned qr code
 */
private void showQrImage() {
    multiFormatWriter = new MultiFormatWriter();
    Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);

    hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    try{
        BarcodeFormat format = generalHandler.StringToBarcodeFormat(qrcodeFormat);
        BitMatrix bitMatrix = multiFormatWriter.encode(qrcode, format, 250,250, hintMap);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        bitmap = barcodeEncoder.createBitmap(bitMatrix);
        codeImage.setImageBitmap(bitmap);
    } catch (Exception e){
        codeImage.setVisibility(View.GONE);
    }
}
 
Example 9
Source File: QRCodeEncode.java    From Tok-Android with GNU General Public License v3.0 6 votes vote down vote up
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    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;
}
 
Example 10
Source File: AddProductFragment.java    From mobikul-standalone-pos with MIT License 5 votes vote down vote up
public void setBarcode(String barCodeNumber) {
    String text = barCodeNumber + ""; // Whatever you need to encode in the QR code
    Log.d(ContentValues.TAG, "generateBarcode: " + text);
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.CODABAR, 380, 100);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
        binding.barCode.setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: AddProductFragment.java    From mobikul-standalone-pos with MIT License 5 votes vote down vote up
public void generateBarcode(Product product) {
    String text = createRandomInteger(); // Whatever you need to encode in the QR code
    product.setBarCode(text);
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.CODABAR, 380, 100);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
        binding.barCode.setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}
 
Example 12
Source File: QRCodeEncoder.java    From zom-android-matrix with Apache License 2.0 5 votes vote down vote up
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    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.setPixel(x,y,result.get(x, y) ? BLACK : WHITE);
        }
    }

    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example 13
Source File: Utils.java    From tron-wallet-android with Apache License 2.0 5 votes vote down vote up
public static Bitmap strToQR(String str, int width, int height) {
    if(str == null || str.equals("")) {
        return null;
    }
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(str, BarcodeFormat.QR_CODE,width,height);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        return barcodeEncoder.createBitmap(bitMatrix);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 14
Source File: QREncoder.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded)
        return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    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;
}
 
Example 15
Source File: RxBarCode.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
public static Bitmap createBarCode(CharSequence content, int BAR_WIDTH, int BAR_HEIGHT, int backgroundColor, int codeColor) {
    /**
     * 条形码的编码类型
     */
    BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
    final int backColor = backgroundColor;
    final int barCodeColor = codeColor;

    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = null;
    try {
        result = writer.encode(content + "", barcodeFormat, BAR_WIDTH, BAR_HEIGHT, null);
    } catch (WriterException e) {
        e.printStackTrace();
    }

    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    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) ? barCodeColor : backColor;
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example 16
Source File: GeneratorResultActivity.java    From SecScanQR with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneralHandler generalHandler = new GeneralHandler(this);
    generalHandler.loadTheme();
    setContentView(R.layout.activity_generator_result);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    codeImage = (ImageView) findViewById(R.id.resultImage);
    btnSave = (Button) findViewById(R.id.btnSave);
    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    text2Code = intent.getStringExtra("CODE");
    formatInt = bundle.getInt("FORMAT");
    format = generalHandler.idToBarcodeFormat(formatInt);
    multiFormatWriter = new MultiFormatWriter();
    Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);

    hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    try{
        BitMatrix bitMatrix = multiFormatWriter.encode(text2Code, format, 1000,1000, hintMap);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        bitmap = barcodeEncoder.createBitmap(bitMatrix);
        codeImage.setImageBitmap(bitmap);
    } catch (Exception e){
        Toast.makeText(getApplicationContext(), getResources().getText(R.string.error_generate), Toast.LENGTH_SHORT).show();
        finish();
    }
    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            requestPermission();
        }
    });

}
 
Example 17
Source File: EncodingUtils.java    From tysq-android with GNU General Public License v3.0 4 votes vote down vote up
/**
     * 绘制条形码
     *
     * @param content       要生成条形码包含的内容
     * @param widthPix      条形码的宽度
     * @param heightPix     条形码的高度
     * @param isShowContent 否则显示条形码包含的内容
     * @return 返回生成条形的位图
     */
    public static Bitmap createBarcode(String content, int widthPix, int heightPix, boolean isShowContent) {
        if (TextUtils.isEmpty(content)) {
            return null;
        }
        //配置参数
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别 这里选择最高H级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        MultiFormatWriter writer = new MultiFormatWriter();

        try {
            // 图像数据转换,使用了矩阵转换 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
            BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.CODE_128, widthPix, heightPix, hints);

            //增加:把宽度修改我们修改过后的真实的宽度
            widthPix = bitMatrix.getWidth();
            // Log.e("zmm", "---------->" + widthPix + "--->" + height);
            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;// 白色
                    }
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
            if (isShowContent) {
                bitmap = showContent(bitmap, content);
            }
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
        }

        return null;
    }
 
Example 18
Source File: CodeUtil.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 *生成带logo的二维码图片
 * @param logoFile /logo图片文件
 * @param codeFile /二维码图片
 * @param qrUrl /二维码存储的信息:vcard格式
 * @param note /二维码描述信息
 */
public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl, String note) {
    try {
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
        BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

        // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
        for (int x = 0; x < WIDTH; x++) {
            for (int y = 0; y < HEIGHT; y++) {
                image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
            }
        }

        int width = image.getWidth();
        int height = image.getHeight();
        if (Objects.nonNull(logoFile) && logoFile.exists()) {
            // 构建绘图对象
            Graphics2D g = image.createGraphics();
            // 读取Logo图片
            BufferedImage logo = ImageIO.read(logoFile);
            // 开始绘制logo图片
            g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
            g.dispose();
            logo.flush();
        }

        // 自定义文本描述
        if (StringUtils.isNotEmpty(note)) {
            // 新的图片,把带logo的二维码下面加上文字
            BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D outg = outImage.createGraphics();
            // 画二维码到新的面板
            outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
            // 画文字到新的面板
            outg.setColor(Color.BLACK);
            outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
            int strWidth = outg.getFontMetrics().stringWidth(note);
            if (strWidth > WIDTH) {
                // //长度过长就截取前面部分
                // 长度过长就换行
                String note1 = note.substring(0, note.length() / 2);
                String note2 = note.substring(note.length() / 2, note.length());
                int strWidth1 = outg.getFontMetrics().stringWidth(note1);
                int strWidth2 = outg.getFontMetrics().stringWidth(note2);
                outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
                BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D outg2 = outImage2.createGraphics();
                outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
                outg2.setColor(Color.BLACK);
                outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号
                outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
                outg2.dispose();
                outImage2.flush();
                outImage = outImage2;
            } else {
                outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字
            }
            outg.dispose();
            outImage.flush();
            image = outImage;
        }

        image.flush();

        ImageIO.write(image, "png", codeFile); // TODO
        if (Objects.nonNull(logoFile) && logoFile.exists()) {
        	logoFile.delete();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 19
Source File: CodeUtil.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 *生成带logo的二维码图片
 * @param logoFile /logo图片文件
 * @param codeFile /二维码图片
 * @param qrUrl /二维码存储的信息:vcard格式
 * @param note /二维码描述信息
 */
public static byte[] drawLogoQRCodeByte(BufferedImage logo, ByteArrayOutputStream codeFile, String qrUrl, String note) {
	byte[] bs = null;
    try {
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
        BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

        // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
        for (int x = 0; x < WIDTH; x++) {
            for (int y = 0; y < HEIGHT; y++) {
                image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
            }
        }

        int width = image.getWidth();
        int height = image.getHeight();
        if (Objects.nonNull(logo)) {
            // 构建绘图对象
            Graphics2D g = image.createGraphics();
            // 读取Logo图片
            //BufferedImage logo = ImageIO.read(logoFile);
            // 开始绘制logo图片
            g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
            g.dispose();
            logo.flush();
        }

        // 自定义文本描述
        if (StringUtils.isNotEmpty(note)) {
            // 新的图片,把带logo的二维码下面加上文字
            BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D outg = outImage.createGraphics();
            // 画二维码到新的面板
            outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
            // 画文字到新的面板
            outg.setColor(Color.BLACK);
            outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
            int strWidth = outg.getFontMetrics().stringWidth(note);
            if (strWidth > WIDTH) {
                // //长度过长就截取前面部分
                // 长度过长就换行
                String note1 = note.substring(0, note.length() / 2);
                String note2 = note.substring(note.length() / 2, note.length());
                int strWidth1 = outg.getFontMetrics().stringWidth(note1);
                int strWidth2 = outg.getFontMetrics().stringWidth(note2);
                outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
                BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D outg2 = outImage2.createGraphics();
                outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
                outg2.setColor(Color.BLACK);
                outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号
                outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
                outg2.dispose();
                outImage2.flush();
                outImage = outImage2;
            } else {
                outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字
            }
            outg.dispose();
            outImage.flush();
            image = outImage;
        }

        image.flush();

        ImageIO.write(image, "png", codeFile); // TODO
        bs = codeFile.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bs;
}
 
Example 20
Source File: BarcodeEncodedFragment.java    From text_converter with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Bitmap doInBackground(Void... params) {
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    try {
        int width = 1024;
        int height = 1024;
        switch (format) {
            case AZTEC:
                break;
            case CODABAR:
                height = (int) (width * 0.4f);
                break;
            case CODE_39:
                height = (int) (width * 0.4f);
                break;
            case CODE_93:
                break;
            case CODE_128:
                height = (int) (width * 0.4f);
                break;
            case DATA_MATRIX:
                break;
            case EAN_8:
                height = (int) (width * 0.4f);
                break;
            case EAN_13:
                height = (int) (width * 0.4f);
                break;
            case ITF:
                height = (int) (width * 0.4f);
                break;
            case MAXICODE:
                break;
            case PDF_417:
                height = (int) (width * 0.4f);
                break;
            case QR_CODE:
                break;
            case RSS_14:
                break;
            case RSS_EXPANDED:
                break;
            case UPC_A:
                break;
            case UPC_E:
                break;
            case UPC_EAN_EXTENSION:
                break;
        }
        BitMatrix bitMatrix = multiFormatWriter.encode(data, format, width, height);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        return barcodeEncoder.createBitmap(bitMatrix);
    } catch (Exception e) {
        e.printStackTrace();
        this.error = e;
    }
    return null;
}