Advertisement
Guest User

QRCodeEncoder

a guest
Jun 16th, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.08 KB | None | 0 0
  1. public final class QRCodeEncoder {
  2.     private static final int WHITE = 0xFFFFFFFF;
  3.     private static final int BLACK = 0xFF000000;
  4.  
  5.     private int dimension = Integer.MIN_VALUE;
  6.     private String contents = null;
  7.     private String displayContents = null;
  8.     private String title = null;
  9.     private BarcodeFormat format = null;
  10.     private boolean encoded = false;
  11.  
  12.     public QRCodeEncoder(String data, Bundle bundle, String type,
  13.             String format, int dimension) {
  14.         this.dimension = dimension;
  15.         encoded = encodeContents(data, bundle, type, format);
  16.     }
  17.  
  18.     public String getContents() {
  19.         return contents;
  20.     }
  21.  
  22.     public String getDisplayContents() {
  23.         return displayContents;
  24.     }
  25.  
  26.     public String getTitle() {
  27.         return title;
  28.     }
  29.  
  30.     private boolean encodeContents(String data, Bundle bundle, String type,
  31.             String formatString) {
  32.         // Default to QR_CODE if no format given.
  33.         format = null;
  34.         if (formatString != null) {
  35.             try {
  36.                 format = BarcodeFormat.valueOf(formatString);
  37.             } catch (IllegalArgumentException iae) {
  38.                 // Ignore it then
  39.             }
  40.         }
  41.         if (format == null || format == BarcodeFormat.QR_CODE) {
  42.             this.format = BarcodeFormat.QR_CODE;
  43.             encodeQRCodeContents(data, bundle, type);
  44.         } else if (data != null && data.length() > 0) {
  45.             contents = data;
  46.             displayContents = data;
  47.             title = "Text";
  48.         }
  49.         return contents != null && contents.length() > 0;
  50.     }
  51.  
  52.     private void encodeQRCodeContents(String data, Bundle bundle, String type) {
  53.         if (type.equals(Contents.Type.TEXT)) {
  54.             if (data != null && data.length() > 0) {
  55.                 contents = data;
  56.                 displayContents = data;
  57.                 title = "Text";
  58.             }
  59.         } else if (type.equals(Contents.Type.EMAIL)) {
  60.             data = trim(data);
  61.             if (data != null) {
  62.                 contents = "mailto:" + data;
  63.                 displayContents = data;
  64.                 title = "E-Mail";
  65.             }
  66.         } else if (type.equals(Contents.Type.PHONE)) {
  67.             data = trim(data);
  68.             if (data != null) {
  69.                 contents = "tel:" + data;
  70.                 displayContents = PhoneNumberUtils.formatNumber(data);
  71.                 title = "Phone";
  72.             }
  73.         } else if (type.equals(Contents.Type.SMS)) {
  74.             data = trim(data);
  75.             if (data != null) {
  76.                 contents = "sms:" + data;
  77.                 displayContents = PhoneNumberUtils.formatNumber(data);
  78.                 title = "SMS";
  79.             }
  80.         } else if (type.equals(Contents.Type.CONTACT)) {
  81.             if (bundle != null) {
  82.                 StringBuilder newContents = new StringBuilder(100);
  83.                 StringBuilder newDisplayContents = new StringBuilder(100);
  84.  
  85.                 newContents.append("MECARD:");
  86.  
  87.                 String name = trim(bundle
  88.                         .getString(ContactsContract.Intents.Insert.NAME));
  89.                 if (name != null) {
  90.                     newContents.append("N:").append(escapeMECARD(name))
  91.                             .append(';');
  92.                     newDisplayContents.append(name);
  93.                 }
  94.  
  95.                 String address = trim(bundle
  96.                         .getString(ContactsContract.Intents.Insert.POSTAL));
  97.                 if (address != null) {
  98.                     newContents.append("ADR:").append(escapeMECARD(address))
  99.                             .append(';');
  100.                     newDisplayContents.append('\n').append(address);
  101.                 }
  102.  
  103.                 Collection<String> uniquePhones = new HashSet<String>(
  104.                         Contents.PHONE_KEYS.length);
  105.                 for (int x = 0; x < Contents.PHONE_KEYS.length; x++) {
  106.                     String phone = trim(bundle
  107.                             .getString(Contents.PHONE_KEYS[x]));
  108.                     if (phone != null) {
  109.                         uniquePhones.add(phone);
  110.                     }
  111.                 }
  112.                 for (String phone : uniquePhones) {
  113.                     newContents.append("TEL:").append(escapeMECARD(phone))
  114.                             .append(';');
  115.                     newDisplayContents.append('\n').append(
  116.                             PhoneNumberUtils.formatNumber(phone));
  117.                 }
  118.  
  119.                 Collection<String> uniqueEmails = new HashSet<String>(
  120.                         Contents.EMAIL_KEYS.length);
  121.                 for (int x = 0; x < Contents.EMAIL_KEYS.length; x++) {
  122.                     String email = trim(bundle
  123.                             .getString(Contents.EMAIL_KEYS[x]));
  124.                     if (email != null) {
  125.                         uniqueEmails.add(email);
  126.                     }
  127.                 }
  128.                 for (String email : uniqueEmails) {
  129.                     newContents.append("EMAIL:").append(escapeMECARD(email))
  130.                             .append(';');
  131.                     newDisplayContents.append('\n').append(email);
  132.                 }
  133.  
  134.                 String url = trim(bundle.getString(Contents.URL_KEY));
  135.                 if (url != null) {
  136.                     // escapeMECARD(url) -> wrong escape e.g.
  137.                     // http\://zxing.google.com
  138.                     newContents.append("URL:").append(url).append(';');
  139.                     newDisplayContents.append('\n').append(url);
  140.                 }
  141.  
  142.                 String note = trim(bundle.getString(Contents.NOTE_KEY));
  143.                 if (note != null) {
  144.                     newContents.append("NOTE:").append(escapeMECARD(note))
  145.                             .append(';');
  146.                     newDisplayContents.append('\n').append(note);
  147.                 }
  148.  
  149.                 // Make sure we've encoded at least one field.
  150.                 if (newDisplayContents.length() > 0) {
  151.                     newContents.append(';');
  152.                     contents = newContents.toString();
  153.                     displayContents = newDisplayContents.toString();
  154.                     title = "Contact";
  155.                 } else {
  156.                     contents = null;
  157.                     displayContents = null;
  158.                 }
  159.  
  160.             }
  161.         } else if (type.equals(Contents.Type.LOCATION)) {
  162.             if (bundle != null) {
  163.                 // These must use Bundle.getFloat(), not getDouble(), it's part
  164.                 // of the API.
  165.                 float latitude = bundle.getFloat("LAT", Float.MAX_VALUE);
  166.                 float longitude = bundle.getFloat("LONG", Float.MAX_VALUE);
  167.                 if (latitude != Float.MAX_VALUE && longitude != Float.MAX_VALUE) {
  168.                     contents = "geo:" + latitude + ',' + longitude;
  169.                     displayContents = latitude + "," + longitude;
  170.                     title = "Location";
  171.                 }
  172.             }
  173.         }
  174.     }
  175.  
  176.     public Bitmap encodeAsBitmap() throws WriterException {
  177.         if (!encoded)
  178.             return null;
  179.  
  180.         Map<EncodeHintType, Object> hints = null;
  181.         String encoding = guessAppropriateEncoding(contents);
  182.         if (encoding != null) {
  183.             hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
  184.             hints.put(EncodeHintType.CHARACTER_SET, encoding);
  185.         }
  186.         MultiFormatWriter writer = new MultiFormatWriter();
  187.         BitMatrix result = writer.encode(contents, format, dimension,
  188.                 dimension, hints);
  189.         int width = result.getWidth();
  190.         int height = result.getHeight();
  191.         int[] pixels = new int[width * height];
  192.         // All are 0, or black, by default
  193.         for (int y = 0; y < height; y++) {
  194.             int offset = y * width;
  195.             for (int x = 0; x < width; x++) {
  196.                 pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
  197.             }
  198.         }
  199.  
  200.         Bitmap bitmap = Bitmap.createBitmap(width, height,
  201.                 Bitmap.Config.ARGB_8888);
  202.         bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
  203.         return bitmap;
  204.     }
  205.  
  206.     private static String guessAppropriateEncoding(CharSequence contents) {
  207.         // Very crude at the moment
  208.         for (int i = 0; i < contents.length(); i++) {
  209.             if (contents.charAt(i) > 0xFF) {
  210.                 return "UTF-8";
  211.             }
  212.         }
  213.         return null;
  214.     }
  215.  
  216.     private static String trim(String s) {
  217.         if (s == null) {
  218.             return null;
  219.         }
  220.         String result = s.trim();
  221.         return result.length() == 0 ? null : result;
  222.     }
  223.  
  224.     private static String escapeMECARD(String input) {
  225.         if (input == null || (input.indexOf(':') < 0 && input.indexOf(';') < 0)) {
  226.             return input;
  227.         }
  228.         int length = input.length();
  229.         StringBuilder result = new StringBuilder(length);
  230.         for (int i = 0; i < length; i++) {
  231.             char c = input.charAt(i);
  232.             if (c == ':' || c == ';') {
  233.                 result.append('\\');
  234.             }
  235.             result.append(c);
  236.         }
  237.         return result.toString();
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement