Exemplo n.º 1
0
 private static Vector<BarcodeFormat> parseDecodeFormats(
     Iterable<String> scanFormats, String decodeMode) {
   if (scanFormats != null) {
     Vector<BarcodeFormat> formats = new Vector<BarcodeFormat>();
     try {
       for (String format : scanFormats) {
         formats.add(BarcodeFormat.valueOf(format));
       }
       return formats;
     } catch (IllegalArgumentException iae) {
       // ignore it then
     }
   }
   if (decodeMode != null) {
     if (Intents.Scan.PRODUCT_MODE.equals(decodeMode)) {
       return PRODUCT_FORMATS;
     }
     if (Intents.Scan.QR_CODE_MODE.equals(decodeMode)) {
       return QR_CODE_FORMATS;
     }
     if (Intents.Scan.DATA_MATRIX_MODE.equals(decodeMode)) {
       return DATA_MATRIX_FORMATS;
     }
     if (Intents.Scan.ONE_D_MODE.equals(decodeMode)) {
       return ONE_D_FORMATS;
     }
   }
   return null;
 }
Exemplo n.º 2
0
 public HistoryItem buildHistoryItem(int number) {
   SQLiteOpenHelper helper = new DBHelper(activity);
   SQLiteDatabase db = null;
   Cursor cursor = null;
   try {
     db = helper.getReadableDatabase();
     cursor =
         db.query(
             DBHelper.TABLE_NAME,
             COLUMNS,
             null,
             null,
             null,
             null,
             DBHelper.TIMESTAMP_COL + " DESC");
     cursor.move(number + 1);
     String text = cursor.getString(0);
     String display = cursor.getString(1);
     String format = cursor.getString(2);
     long timestamp = cursor.getLong(3);
     String details = cursor.getString(4);
     Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
     return new HistoryItem(result, display, details);
   } finally {
     close(cursor, db);
   }
 }
Exemplo n.º 3
0
 public List<HistoryItem> buildHistoryItems() {
   SQLiteOpenHelper helper = new DBHelper(activity);
   List<HistoryItem> items = new ArrayList<>();
   SQLiteDatabase db = null;
   Cursor cursor = null;
   try {
     db = helper.getReadableDatabase();
     cursor =
         db.query(
             DBHelper.TABLE_NAME,
             COLUMNS,
             null,
             null,
             null,
             null,
             DBHelper.TIMESTAMP_COL + " DESC");
     while (cursor.moveToNext()) {
       String text = cursor.getString(0);
       String display = cursor.getString(1);
       String format = cursor.getString(2);
       long timestamp = cursor.getLong(3);
       String details = cursor.getString(4);
       Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
       items.add(new HistoryItem(result, display, details));
     }
   } finally {
     close(cursor, db);
   }
   return items;
 }
Exemplo n.º 4
0
 // It would be nice if the string encoding lived in the core ZXing library,
 // but we use platform specific code like PhoneNumberUtils, so it can't.
 private boolean encodeContentsFromZXingIntent(Intent intent) {
   // Default to QR_CODE if no format given.
   String formatString = intent.getStringExtra(Intents.Encode.FORMAT);
   format = null;
   if (formatString != null) {
     try {
       format = BarcodeFormat.valueOf(formatString);
     } catch (IllegalArgumentException iae) {
       // Ignore it then
     }
   }
   if (format == null || format == BarcodeFormat.QR_CODE) {
     String type = intent.getStringExtra(Intents.Encode.TYPE);
     if (type == null || type.isEmpty()) {
       return false;
     }
     this.format = BarcodeFormat.QR_CODE;
     encodeQRCodeContents(intent, type);
   } else {
     String data = intent.getStringExtra(Intents.Encode.DATA);
     if (data != null && !data.isEmpty()) {
       contents = data;
       displayContents = data;
     }
   }
   return contents != null && !contents.isEmpty();
 }
Exemplo n.º 5
0
  private boolean decode(
      MonochromeBitmapSource source, float rotation, String expectedText, boolean tryHarder) {
    Result result;
    String suffix = " (" + (tryHarder ? "try harder, " : "") + "rotation: " + rotation + ')';

    try {
      result = barcodeReader.decode(source, tryHarder ? TRY_HARDER_HINT : null);
    } catch (ReaderException re) {
      System.out.println(re + suffix);
      return false;
    }

    if (!expectedFormat.equals(result.getBarcodeFormat())) {
      System.out.println(
          "Format mismatch: expected '"
              + expectedFormat
              + "' but got '"
              + result.getBarcodeFormat()
              + "'"
              + suffix);
      return false;
    }

    String resultText = result.getText();
    if (!expectedText.equals(resultText)) {
      System.out.println(
          "Mismatch: expected '" + expectedText + "' but got '" + resultText + "'" + suffix);
      return false;
    }
    return true;
  }
Exemplo n.º 6
0
  private static MatrixToImageResult matrixToImage(BitMatrix matrix, BarcodeFormat type) {
    if (matrix != null) {
      try {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

        int onColor = 0xFF000000;
        int offColor = 0xFFFFFFFF;
        int[] pixels = new int[width * height];
        int index = 0;
        for (int y = 0; y < height; y++) {
          for (int x = 0; x < width; x++) {
            pixels[index++] = matrix.get(x, y) ? onColor : offColor;
          }
        }
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

        File file =
            new File(
                Extension.mainContext.getApplicationInfo().dataDir,
                "code_" + type.toString() + ".jpg");
        FileOutputStream out = new FileOutputStream(file);
        if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
          out.flush();
          out.close();
          return new MatrixToImageResult(true, file.getAbsolutePath());
        } else return new MatrixToImageResult(false, null);
      } catch (Exception e) {
        e.printStackTrace();
        return new MatrixToImageResult(false, null);
      }
    }
    return new MatrixToImageResult(false, null);
  }
 private static Set<BarcodeFormat> parseDecodeFormats(
     Iterable<String> scanFormats, String decodeMode) {
   if (scanFormats != null) {
     Set<BarcodeFormat> formats = EnumSet.noneOf(BarcodeFormat.class);
     try {
       for (String format : scanFormats) {
         formats.add(BarcodeFormat.valueOf(format));
       }
       return formats;
     } catch (IllegalArgumentException iae) {
       // ignore it then
     }
   }
   if (decodeMode != null) {
     return FORMATS_FOR_MODE.get(decodeMode);
   }
   return null;
 }
 private boolean encodeContents(String data, Bundle bundle, String type, String formatString) {
   // Default to QR_CODE if no format given.
   format = null;
   if (formatString != null) {
     try {
       format = BarcodeFormat.valueOf(formatString);
     } catch (IllegalArgumentException iae) {
       // Ignore it then
     }
   }
   if (format == null || format == BarcodeFormat.QR_CODE) {
     this.format = BarcodeFormat.QR_CODE;
     encodeQRCodeContents(data, bundle, type);
   } else if (data != null && data.length() > 0) {
     contents = data;
     displayContents = data;
     title = "Text";
   }
   return contents != null && contents.length() > 0;
 }
Exemplo n.º 9
0
  public static void encode(java.lang.String content, int type, int width, int height) {

    try {
      Map<EncodeHintType, Object> hints = new HashMap<>();
      BarcodeFormat format = BarcodeFormat.QR_CODE;
      switch (type) {
        case 0:
          format = BarcodeFormat.AZTEC;
          break;

        case 1:
          format = BarcodeFormat.CODABAR;
          break;

        case 2:
          format = BarcodeFormat.CODE_39;
          break;

        case 3:
          format = BarcodeFormat.CODE_93;
          break;

        case 4:
          format = BarcodeFormat.CODE_128;
          break;

        case 5:
          format = BarcodeFormat.DATA_MATRIX;
          break;

        case 6:
          format = BarcodeFormat.EAN_8;
          break;

        case 7:
          format = BarcodeFormat.EAN_13;
          break;

        case 8:
          format = BarcodeFormat.ITF;
          break;

        case 9:
          format = BarcodeFormat.MAXICODE;
          break;

        case 10:
          format = BarcodeFormat.PDF_417;
          break;

        case 11:
          format = BarcodeFormat.QR_CODE;
          break;

        case 12:
          format = BarcodeFormat.RSS_14;
          break;

        case 13:
          format = BarcodeFormat.RSS_EXPANDED;
          break;

        case 14:
          format = BarcodeFormat.UPC_A;
          break;

        case 15:
          format = BarcodeFormat.UPC_E;
          break;

        case 16:
          format = BarcodeFormat.UPC_EAN_EXTENSION;
          break;
      }
      Trace.Info(format.toString() + " generation start");
      BitMatrix matrix = new MultiFormatWriter().encode(content, format, width, height, hints);

      MatrixToImageResult image = matrixToImage(matrix, format);
      HaxeCallback.DispatchEventToHaxe(
          "qrscan.QRScanEvent", new Object[] {"generated", format.toString(), image.path});
    } catch (Exception e) {
      Log.d("trace", e.toString());
    }
  }