本文介绍了如何使用新的Google Vision API生成条码并将其转换为位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用新的Google Vision API生成条形码并将其转换为位图?

  Barcode barcode = new Barcode() ; 
Barcode.Email email = new Barcode.Email();
email.address [email protected];
email.subject =我的主题;
email.body =我的身体内容;
barcode.email =电子邮件;

//实现转换
位图barcodeImage = barcodeToBitmap(barcode); //我知道这部分。

您可以尝试类似的方式,它使用zxing库: / b>

  public static Bitmap getBitmap(String barcode,int barcodeType,int width,int height)
{
Bitmap barcodeBitmap = null;
BarcodeFormat barcodeFormat = convertToZXingFormat(barcodeType);
try
{
barcodeBitmap = encodeAsBitmap(barcode,barcodeFormat,width,height);
}
catch(WriterException e)
{
e.printStackTrace();
}
返回barcodeBitmap;
}

private static BarcodeFormat convertToZXingFormat(int form at)
{
switch(format)
{
case 8:
return BarcodeFormat.CODABAR;
案例1:
返回BarcodeFormat.CODE_128;
案例2:
返回BarcodeFormat.CODE_39;
案例4:
返回BarcodeFormat.CODE_93;
案例32:
返回BarcodeFormat.EAN_13;
情况64:
返回BarcodeFormat.EAN_8;
case 128:
返回BarcodeFormat.ITF;
情况512:
返回BarcodeFormat.UPC_A;
case 1024:
return BarcodeFormat.UPC_E;
//默认128?
默认值:
返回BarcodeFormat.CODE_128;
}
}


/ *************************** ***********************************
*从com.google.zxing.client获取。 android.encode.QRCodeEncoder
*
*请参阅下面的网站
* http://code.google.com/p/zxing/
* http://code.google .com / p / zxing / source / browse / trunk / android / src / com / google / zxing / client / android / encode / EncodeActivity.java
* http://code.google.com/p/zxing /source/browse/trunk/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java
* /

private static final int WHITE = 0xFFFFFFFF;
private static final int BLACK = 0xFF000000;

私有静态位图encodeAsBitmap(字符串内容,BarcodeFormat格式,int img_width,int img_height)抛出WriterException
{
if(contents == null){
return null ;
}
映射< EncodeHintType,Object> hints = null;
字符串编码= 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,img_width,img_height,hints);
} catch(IllegalArgumentException iae){
//不支持的格式
return null;
}
int width = result.getWidth();
int height = result.getHeight();
int [] pixels = new int [width * height];
for(int y = 0; y int offset = y * width;
for(int x = 0; x< width; x ++){
pixels [offset + x] = result.get(x,y)?黑,白;
}
}

位图位图= Bitmap.createBitmap(width,height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(像素,0,宽度,0,0,宽度,高度);
返回位图;
}

private static String guessAppropriateEncoding(CharSequence contents){
//此时非常粗糙
for(int i = 0; i< contents.length (); i ++){
if(contents.charAt(i)> 0xFF){
returnUTF-8;
}
}
返回null;
}

}


How to generate an Barcode and convert it to Bitmap using new Google Vision API?

Barcode barcode = new Barcode();
Barcode.Email email = new Barcode.Email();
email.address = "[email protected]";
email.subject = "My Subject;
email.body = "My body content.";
barcode.email = email;

//Implement conversion Bitmap barcodeImage = barcodeToBitmap(barcode);// I do know this part.

解决方案

You can detect your barcodes using Google Vision API and then use ZXing to generate the barcodes.You could try something like this, it uses zxing library:

public static Bitmap getBitmap(String barcode, int barcodeType, int width, int height)
{
    Bitmap barcodeBitmap = null;
    BarcodeFormat barcodeFormat = convertToZXingFormat(barcodeType);
    try
    {
        barcodeBitmap = encodeAsBitmap(barcode, barcodeFormat, width, height);
    }
    catch (WriterException e)
    {
        e.printStackTrace();
    }
    return barcodeBitmap;
}

private static BarcodeFormat convertToZXingFormat(int format)
{
    switch (format)
    {
        case 8:
            return BarcodeFormat.CODABAR;
        case 1:
            return BarcodeFormat.CODE_128;
        case 2:
            return BarcodeFormat.CODE_39;
        case 4:
            return BarcodeFormat.CODE_93;
        case 32:
            return BarcodeFormat.EAN_13;
        case 64:
            return BarcodeFormat.EAN_8;
        case 128:
            return BarcodeFormat.ITF;
        case 512:
            return BarcodeFormat.UPC_A;
        case 1024:
            return BarcodeFormat.UPC_E;
        //default 128?
        default:
            return BarcodeFormat.CODE_128;
    }
}


/**************************************************************
 * getting from com.google.zxing.client.android.encode.QRCodeEncoder
 *
 * See the sites below
 * http://code.google.com/p/zxing/
 * http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/encode/EncodeActivity.java
 * http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java
 */

private static final int WHITE = 0xFFFFFFFF;
private static final int BLACK = 0xFF000000;

private static Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException
{
    if (contents == null) {
        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;
    try {
        result = writer.encode(contents, format, img_width, img_height, 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;
}

private static String guessAppropriateEncoding(CharSequence contents) {
    // Very crude at the moment
    for (int i = 0; i < contents.length(); i++) {
        if (contents.charAt(i) > 0xFF) {
            return "UTF-8";
        }
    }
    return null;
}

}

这篇关于如何使用新的Google Vision API生成条码并将其转换为位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 10:14