使用zxing lib丢失数字的ITF生成条形码

时间:2016-02-26 20:43:36

标签: java android generator barcode zxing

嗨所以我有这个项目,我想用ZXING lib生成一个ITF-14条形码。 好的,所以我得到它来生成条形码,但我必须手动将数字写入文本视图以显示数字。我希望用位图生成数字,但我不知道该怎么做。
我想生成一些看起来像这样的东西: barcode

但我得到的只是条形码,下面没有数字。我可能错过了zxing libs的一个选项。

所以这是我到目前为止的代码:

import android.graphics.Bitmap;
import android.media.Image;
import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import java.util.EnumMap;
import java.util.Map;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
 import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;


   public class LoginPage extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_page);
    String barCodeData = "123456";
    Bitmap barcode = null;
    Bitmap b2 = null;
    ImageView imageview = (ImageView)findViewById(R.id.barcode);
    TextView num = (TextView)findViewById(R.id.number);

    try{
        barcode = encodeAsBitmap(barCodeData, BarcodeFormat.CODE_128, 600, 300);
        //this works
        b2 = encodeAsBitmap(barCodeData, BarcodeFormat.ITF, 600, 300);
        //this also work but makes the same thing?
        imageview.setImageBitmap(b2);
        num.setText(barCodeData);

    }catch (WriterException e)
    {
        Log.d("Barcode Generation"," Failed to create");
    }

}

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

Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) 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, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result;
    try {
        result = writer.encode(contentsToEncode, 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;
}

}

0 个答案:

没有答案