Android Open Source - android-pkpass-viewer Barcode Encoder






From Project

Back to project page android-pkpass-viewer.

License

The source code is released under:

MIT License

If you think the Android project android-pkpass-viewer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.manso.mytickets.utils;
/*  ww  w .  j  a  v  a 2 s.com*/
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;

public class BarcodeEncoder {
  private static final int WHITE = 0xFFFFFFFF;
  private static final int BLACK = 0xFF000000;
  private Context context;
  
  public BarcodeEncoder(Context context) {
    this.context = context;
  }

  public Bitmap getBitmap(String toEncode, String format) throws WriterException {
    
    com.google.zxing.BarcodeFormat zxingFormat = null;
    
    if (format.equals("PKBarcodeFormatPDF417")) {
      zxingFormat = BarcodeFormat.PDF_417;
    } else if (format.equals("PKBarcodeFormatQR")) {
      zxingFormat = BarcodeFormat.QR_CODE;
    }
    
    com.google.zxing.Writer writer = new MultiFormatWriter();
    
    DisplayMetrics metric = this.context.getResources().getDisplayMetrics();
    float xdpi = metric.xdpi;
    
    BitMatrix result = writer.encode(toEncode, zxingFormat, (int)(xdpi * 0.625), (int)(xdpi * 0.625));

    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 barcode = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    barcode.setPixels(pixels, 0, width, 0, 0, width, height);
    return barcode;    
  }
}




Java Source Code List

com.manso.mytickets.HandleIntentAsyncTask.java
com.manso.mytickets.TicketBackFragment.java
com.manso.mytickets.TicketFrontFragment.java
com.manso.mytickets.ViewTicketFrontActivity.java
com.manso.mytickets.services.EventTicketPassStrategy.java
com.manso.mytickets.services.IPassStrategy.java
com.manso.mytickets.services.ManifestService.java
com.manso.mytickets.services.PassReadingService.java
com.manso.mytickets.services.PassStorageService.java
com.manso.mytickets.services.PassStrategyService.java
com.manso.mytickets.services.PassTypes.java
com.manso.mytickets.utils.BarcodeEncoder.java
com.manso.mytickets.utils.FileReader.java
com.manso.mytickets.utils.ImageUtils.java
com.manso.mytickets.utils.RandomNameGenerator.java
com.manso.mytickets.utils.Sha1.java