Android Zxingは、文字付きの1次元コード(バーコード)を作成します.

3690 ワード

/**
 *                 :                ,     jar    
 *
 * @param content           
 * @return          bitmap
 * @throws WriterException WriterException  
 */
public static Bitmap CreateOneDCode(String content, int w, int h) throws WriterException {
    BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, w, h);

    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, 0)) {
                pixels[y * width + x] = 0xff000000;
            }
            else pixels[y * width + x] = 0xffffffff;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

/**
 *         
 * @param text
 * @param width
 * @param height
 * @return
 */
private  static final int TXT_SIZE = 24;
private  static final int y_offset = 10;
public static Bitmap StringToBitmap(String text, int width, int height) {
    Bitmap newBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawBitmap(newBitmap, 0, 0, null);
    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setTextSize(TXT_SIZE);
    textPaint.setColor(Color.BLACK);
    StaticLayout sl= new StaticLayout(text, textPaint, newBitmap.getWidth()-8, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
    canvas.translate(0, y_offset);
    canvas.drawColor(Color.WHITE);
    sl.draw(canvas);
    return newBitmap;
}


/**
 *     bitmap
 *         
 *       1.       ,           。   imageview     ,           ,         
 *2.           。             
 * @param bit1
 * @param bit2
 * @return
 */
public static Bitmap CombBitmap(Bitmap bit1, Bitmap bit2) {
    Bitmap newBit = null;
    int width = bit1.getWidth();
    if (bit2.getWidth() != width) {
        int h2 = bit2.getHeight() * width / bit2.getWidth();
        newBit = Bitmap.createBitmap(width, bit1.getHeight() + h2, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBit);
        Bitmap newSizeBitmap2 = getNewSizeBitmap(bit2, width, h2);
        canvas.drawBitmap(bit1, 0, 0, null);
        canvas.drawBitmap(newSizeBitmap2, 0, bit1.getHeight(), null);
    } else {
        newBit = Bitmap.createBitmap(width, bit1.getHeight() + bit2.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBit);
        canvas.drawBitmap(bit1, 0, 0, null);
        canvas.drawBitmap(bit2, 0, bit1.getHeight(), null);
    }
    return newBit;
}

/**
 *     bitmap
 * @param bitmap
 * @param newWidth
 * @param newHeight
 * @return
 */
public static Bitmap getNewSizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    float scaleWidth = ((float) newWidth) / bitmap.getWidth();
    float scaleHeight = ((float) newHeight) / bitmap.getHeight();
    //        matrix  
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    //       
    Bitmap bit1Scale = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
            true);
    return bit1Scale;
}

/**
 *           
 * @param content
 * @param w
 * @param h
 * @param stringPrefix
 * @return
 * @throws WriterException
 */
public static Bitmap CreateOneDCodeAndString(String content, int w, int h, String stringPrefix) throws WriterException {
    return CombBitmap(CreateOneDCode(content, w, h-TXT_SIZE-y_offset), StringToBitmap(stringPrefix + content, w, TXT_SIZE + y_offset));
}