/** * Initialize the barcode decoder. */ private void initBarcodeReader(List<String> barCodeTypes) { EnumMap<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class); EnumSet<BarcodeFormat> decodeFormats = EnumSet.noneOf(BarcodeFormat.class); if (barCodeTypes != null) { for (String code : barCodeTypes) { BarcodeFormat format = parseBarCodeString(code); if (format != null) { decodeFormats.add(format); } } } hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats); _multiFormatReader.setHints(hints); }
/** * 条形码编码 */ public static BufferedImage encode(String contents, int width, int height) { int codeWidth = 3 + // start guard (7 * 6) + // left bars 5 + // middle guard (7 * 6) + // right bars 3; // end guard codeWidth = Math.max(codeWidth, width); try { // 原为13位Long型数字(BarcodeFormat.EAN_13),现为128位 BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.CODE_128, codeWidth, height, null); return MatrixToImageWriter.toBufferedImage(bitMatrix); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * Superimpose a line for 1D or dots for 2D to highlight the key features of the barcode. * * @param barcode A bitmap of the captured image. * @param scaleFactor amount by which thumbnail was scaled * @param rawResult The decoded results which contains the points to draw. */ private void drawResultPoints(Bitmap barcode, float scaleFactor, Result rawResult) { ResultPoint[] points = rawResult.getResultPoints(); if (points != null && points.length > 0) { Canvas canvas = new Canvas(barcode); Paint paint = new Paint(); paint.setColor(getResources().getColor(R.color.result_points)); if (points.length == 2) { paint.setStrokeWidth(4.0f); drawLine(canvas, paint, points[0], points[1], scaleFactor); } else if (points.length == 4 && (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A || rawResult.getBarcodeFormat() == BarcodeFormat.EAN_13)) { // Hacky special case -- draw two lines, for the barcode and metadata drawLine(canvas, paint, points[0], points[1], scaleFactor); drawLine(canvas, paint, points[2], points[3], scaleFactor); } else { paint.setStrokeWidth(10.0f); for (ResultPoint point : points) { if (point != null) { canvas.drawPoint(scaleFactor * point.getX(), scaleFactor * point.getY(), paint); } } } } }
private void updateQrCode(String qrData) { DisplayMetrics displayMetrics = new DisplayMetrics(); WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut wm.getDefaultDisplay().getMetrics(displayMetrics); int screenWidth = displayMetrics.widthPixels; int screenHeight = displayMetrics.heightPixels; int qrCodeDimension = screenWidth > screenHeight ? screenHeight : screenWidth; QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(qrData, null, Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(), qrCodeDimension); try { mBitmap = qrCodeEncoder.encodeAsBitmap(); mImageView.setImageBitmap(mBitmap); mImageView.setVisibility(View.VISIBLE); mErrorView.setVisibility(View.GONE); } catch (WriterException e) { e.printStackTrace(); mErrorView.setText("Error: "+e.getMessage()); mErrorView.setVisibility(View.VISIBLE); mImageView.setVisibility(View.GONE); } }
public static String createQrcode(String dir, String _text) { String qrcodeFilePath = ""; try { int qrcodeWidth = 300; int qrcodeHeight = 300; String qrcodeFormat = "png"; HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(_text, BarcodeFormat.QR_CODE, qrcodeWidth, qrcodeHeight, hints); BufferedImage image = new BufferedImage(qrcodeWidth, qrcodeHeight, BufferedImage.TYPE_INT_RGB); File qrcodeFile = new File(dir + "/" + UUID.randomUUID().toString() + "." + qrcodeFormat); ImageIO.write(image, qrcodeFormat, qrcodeFile); MatrixToImageWriter.writeToPath(bitMatrix, qrcodeFormat, qrcodeFile.toPath()); qrcodeFilePath = qrcodeFile.getAbsolutePath(); } catch (Exception e) { e.printStackTrace(); } return qrcodeFilePath; }
private Bitmap endcode(String input) { BarcodeFormat format = BarcodeFormat.QR_CODE; EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hint.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix result = null; try { result = new MultiFormatWriter().encode(input, BarcodeFormat.QR_CODE, 500, 500, hint); } catch (WriterException e) { e.printStackTrace(); } int w = result.getWidth(); int h = result.getHeight(); int[] pixels = new int[w * h]; for (int y = 0; y < h; y++) { int offset = y * w; for (int x = 0; x < w; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } Bitmap bit = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); bit.setPixels(pixels, 0, w, 0, 0, w, h); ImageView imageView = (ImageView) findViewById(R.id.qr_code_id); imageView.setImageBitmap(bit); return bit; }
/** * 解析二维码图片工具类 * * @param bitmap */ public static String analyzeBitmap(Bitmap bitmap) { MultiFormatReader multiFormatReader = new MultiFormatReader(); // 解码的参数 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(2); // 可以解析的编码类型 Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>(); if (decodeFormats == null || decodeFormats.isEmpty()) { decodeFormats = new Vector<BarcodeFormat>(); // 这里设置可扫描的类型,我这里选择了都支持 decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS); decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS); decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS); } hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats); // 设置继续的字符编码格式为UTF8 hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); // 设置解析配置参数 multiFormatReader.setHints(hints); // 开始对图像资源解码 Result rawResult = null; try { rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(bitmap)))); } catch (Exception e) { e.printStackTrace(); } if (rawResult != null) { return rawResult.getText(); } else { return "Failed"; } }
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); 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, y)) { pixels[y * width + x] = BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
/** * 生成条形二维码 */ public static byte[] encode(String contents, int width, int height, String imgPath) { int codeWidth = 3 + // start guard (7 * 6) + // left bars 5 + // middle guard (7 * 6) + // right bars 3; // end guard codeWidth = Math.max(codeWidth, width); byte[] buf = null; try { BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.CODE_128, codeWidth, height, null); BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(image, FORMAT_NAME, out); out.close(); buf = out.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return buf; }
CaptureActivityHandler(CaptureActivity activity, Collection<BarcodeFormat> decodeFormats, Map<DecodeHintType,?> baseHints, String characterSet, CameraManager cameraManager) { this.activity = activity; decodeThread = new DecodeThread(activity, decodeFormats, baseHints, characterSet, new ViewfinderResultPointCallback(activity.getViewfinderView())); decodeThread.start(); state = State.SUCCESS; // Start ourselves capturing previews and decoding. this.cameraManager = cameraManager; cameraManager.startPreview(); restartPreviewAndDecode(); }
public ProductParsedResult parse(Result result) { BarcodeFormat format = result.getBarcodeFormat(); if (format != BarcodeFormat.UPC_A && format != BarcodeFormat.UPC_E && format != BarcodeFormat.EAN_8 && format != BarcodeFormat.EAN_13) { return null; } String rawText = ResultParser.getMassagedText(result); if (!ResultParser.isStringOfDigits(rawText, rawText.length())) { return null; } String normalizedProductID; if (format == BarcodeFormat.UPC_E && rawText.length() == 8) { normalizedProductID = UPCEReader.convertUPCEtoUPCA(rawText); } else { normalizedProductID = rawText; } return new ProductParsedResult(rawText, normalizedProductID); }
private boolean encodeContentsFromZXingIntent(Intent intent) { // Default to QR_CODE if no format given. String formatString = intent.getStringExtra(Intents.Encode.FORMAT); try { format = BarcodeFormat.valueOf(formatString); } catch (IllegalArgumentException iae) { // Ignore it then format = null; } if (format == null || BarcodeFormat.QR_CODE.equals(format)) { String type = intent.getStringExtra(Intents.Encode.TYPE); if (type == null || type.length() == 0) { return false; } this.format = BarcodeFormat.QR_CODE; encodeQRCodeContents(intent, type); } else { String data = intent.getStringExtra(Intents.Encode.DATA); if (data != null && data.length() > 0) { contents = data; displayContents = data; title = activity.getString(R.string.contents_text); } } return contents != null && contents.length() > 0; }
/** * Create QR code * * @param content content * @param widthPix widthPix * @param heightPix heightPix * @param openErrorCorrectionLevel Does fault tolerance open? * @param logoBitmap The two-dimensional code Logo icon (Center for null) * @param filePath The file path used to store two-dimensional code images * @return Generate two-dimensional code and save the file is successful [boolean] */ public static boolean createQRCode(String content, int widthPix, int heightPix, boolean openErrorCorrectionLevel, Bitmap logoBitmap, String filePath) { try { if (TextUtils.isEmpty(content) || TextUtils.equals("null", content) || "".equals(content)) { return false; } Map hints = openErrorCorrectionLevel(openErrorCorrectionLevel); // Image data conversion, the use of matrix conversion BitMatrix bitMatrix = new QRCodeWriter().encode(new String(content.getBytes("UTF-8"), "iso-8859-1"), BarcodeFormat.QR_CODE, widthPix, heightPix, hints); Bitmap bitmap = generateQRBitmap(bitMatrix); if (logoBitmap != null) { bitmap = addLogo(bitmap, logoBitmap); } boolean compress = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath)); //You must use the compress method to save the bitmap to the file and then read it. //The bitmap returned directly is without any compression, and the memory consumption is huge! return bitmap != null && bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath)); } catch (WriterException | IOException e) { e.printStackTrace(); } return false; }
public static @NonNull Bitmap create(String data) { try { BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, 512, 512); Bitmap bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888); for (int y = 0; y < result.getHeight(); y++) { for (int x = 0; x < result.getWidth(); x++) { if (result.get(x, y)) { bitmap.setPixel(x, y, Color.BLACK); } } } return bitmap; } catch (WriterException e) { Log.w(TAG, e); return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888); } }
/**条形码的生成*/ private void createBarCode(HttpServletResponse response){ int codeWidth = 3 + // start guard (7 * 6) + // left bars 5 + // middle guard (7 * 6) + // right bars 3; // end guard codeWidth = Math.max(codeWidth, width); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.CODE_128, codeWidth, height, null); // 条形码 MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream()); } catch (Exception e) { System.out.println("生成条形码异常!"+e.toString()); } }
/** * 传入字符串生成二维码 * * @param str * @return * @throws WriterException */ public static Bitmap Create2DCode(String str) throws WriterException { // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败 BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300, 300); 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, y)) { pixels[y * width + x] = 0xff000000; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 通过像素数组生成bitmap,具体参考api bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
public static Bitmap createQRCode(String content, int widthAndHeight) { if (TextUtils.isEmpty(content)) return null; try { Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //Fault tolerance level. MAX hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //Set the width of the blank margin. hints.put(EncodeHintType.MARGIN, 0); BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); 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, y)) { pixels[y * width + x] = 0xff000000; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; }
public MultiFormatUPCEANReader(Map<DecodeHintType,?> hints) { @SuppressWarnings("unchecked") Collection<BarcodeFormat> possibleFormats = hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS); Collection<UPCEANReader> readers = new ArrayList<>(); if (possibleFormats != null) { if (possibleFormats.contains(BarcodeFormat.EAN_13)) { readers.add(new EAN13Reader()); } else if (possibleFormats.contains(BarcodeFormat.UPC_A)) { readers.add(new UPCAReader()); } if (possibleFormats.contains(BarcodeFormat.EAN_8)) { readers.add(new EAN8Reader()); } if (possibleFormats.contains(BarcodeFormat.UPC_E)) { readers.add(new UPCEReader()); } } if (readers.isEmpty()) { readers.add(new EAN13Reader()); // UPC-A is covered by EAN-13 readers.add(new EAN8Reader()); readers.add(new UPCEReader()); } this.readers = readers.toArray(new UPCEANReader[readers.size()]); }
BarcodeReaderHandler(BarcodeReaderView barcodeReaderView, Collection<BarcodeFormat> decodeFormats, Map<DecodeHintType, ?> baseHints, String characterSet, ResultPointCallback resultPointCallback, CameraManager cameraManager) { this.barcodeReaderView = barcodeReaderView; decodeThread = new DecodeThread(barcodeReaderView, decodeFormats, baseHints, characterSet, resultPointCallback); decodeThread.start(); state = State.SUCCESS; // Start ourselves capturing previews and decoding. this.cameraManager = cameraManager; cameraManager.startPreview(); restartPreviewAndDecode(); }
Result decodeRow(int rowNumber, BitArray row, int[] extensionStartRange) throws NotFoundException { StringBuilder result = this.decodeRowStringBuffer; result.setLength(0); int end = decodeMiddle(row, extensionStartRange, result); String resultString = result.toString(); Map<ResultMetadataType, Object> extensionData = parseExtensionString(resultString); Result extensionResult = new Result(resultString, null, new ResultPoint[]{new ResultPoint (((float) (extensionStartRange[0] + extensionStartRange[1])) / 2.0f, (float) rowNumber), new ResultPoint((float) end, (float) rowNumber)}, BarcodeFormat.UPC_EAN_EXTENSION); if (extensionData != null) { extensionResult.putAllMetadata(extensionData); } return extensionResult; }
/** * Superimpose a line for 1D or dots for 2D to highlight the key features of * the barcode. * * @param barcode * A bitmap of the captured image. * @param scaleFactor * amount by which thumbnail was scaled * @param rawResult * The decoded results which contains the points to draw. */ private void drawResultPoints(Bitmap barcode, float scaleFactor, Result rawResult) { ResultPoint[] points = rawResult.getResultPoints(); if (points != null && points.length > 0) { Canvas canvas = new Canvas(barcode); Paint paint = new Paint(); paint.setColor(Color.parseColor("#c099cc00")); if (points.length == 2) { paint.setStrokeWidth(4.0f); drawLine(canvas, paint, points[0], points[1], scaleFactor); } else if (points.length == 4 && (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A || rawResult .getBarcodeFormat() == BarcodeFormat.EAN_13)) { // Hacky special case -- draw two lines, for the barcode and // metadata drawLine(canvas, paint, points[0], points[1], scaleFactor); drawLine(canvas, paint, points[2], points[3], scaleFactor); } else { paint.setStrokeWidth(10.0f); for (ResultPoint point : points) { if (point != null) { canvas.drawPoint(scaleFactor * point.getX(), scaleFactor * point.getY(), paint); } } } } }
private static Result constructResult(Pair leftPair, Pair rightPair) { int i; String text = String.valueOf((4537077 * ((long) leftPair.getValue())) + ((long) rightPair .getValue())); StringBuilder buffer = new StringBuilder(14); for (i = 13 - text.length(); i > 0; i--) { buffer.append('0'); } buffer.append(text); int checkDigit = 0; for (i = 0; i < 13; i++) { int digit = buffer.charAt(i) - 48; if ((i & 1) == 0) { digit *= 3; } checkDigit += digit; } checkDigit = 10 - (checkDigit % 10); if (checkDigit == 10) { checkDigit = 0; } buffer.append(checkDigit); ResultPoint[] leftPoints = leftPair.getFinderPattern().getResultPoints(); ResultPoint[] rightPoints = rightPair.getFinderPattern().getResultPoints(); return new Result(String.valueOf(buffer.toString()), null, new ResultPoint[]{leftPoints[0], leftPoints[1], rightPoints[0], rightPoints[1]}, BarcodeFormat.RSS_14); }
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; }
static Set<BarcodeFormat> parseDecodeFormats(Intent intent) { Iterable<String> scanFormats = null; CharSequence scanFormatsString = intent.getStringExtra(Intents.Scan.FORMATS); if (scanFormatsString != null) { scanFormats = Arrays.asList(COMMA_PATTERN.split(scanFormatsString)); } return parseDecodeFormats(scanFormats, intent.getStringExtra(Intents.Scan.MODE)); }
public CaptureActivityHandler(CaptureActivity activity, Vector<BarcodeFormat> decodeFormats, String characterSet, ViewfinderView viewfinderView, DecodeCallback decodeCallback) { this.activity = activity; this.decodeCallback = decodeCallback; decodeThread = new DecodeThread(activity, decodeFormats, characterSet, new ViewfinderResultPointCallback(viewfinderView)); decodeThread.start(); state = State.SUCCESS; // Start ourselves capturing previews and decoding. CameraManager.get().startPreview(); restartPreviewAndDecode(); }
static Vector<BarcodeFormat> parseDecodeFormats(Intent intent) { List<String> scanFormats = null; String scanFormatsString = intent.getStringExtra(Intents.Scan.SCAN_FORMATS); if (scanFormatsString != null) { scanFormats = Arrays.asList(COMMA_PATTERN.split(scanFormatsString)); } return parseDecodeFormats(scanFormats, intent.getStringExtra(Intents.Scan.MODE)); }
static Vector<BarcodeFormat> parseDecodeFormats(Uri inputUri) { List<String> formats = inputUri.getQueryParameters(Intents.Scan.SCAN_FORMATS); if (formats != null && formats.size() == 1 && formats.get(0) != null){ formats = Arrays.asList(COMMA_PATTERN.split(formats.get(0))); } return parseDecodeFormats(formats, inputUri.getQueryParameter(Intents.Scan.MODE)); }
private Image buildImage(BarcodeFormat format,String data,int w,int h){ try{ Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.MARGIN,0); if(format.equals(BarcodeFormat.QR_CODE)){ hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); } BitMatrix matrix = new MultiFormatWriter().encode(data,format, w, h,hints); int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); } } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(image, "png", outputStream); byte[] bytes=outputStream.toByteArray(); String base64Data=Base64Utils.encodeToString(bytes); IOUtils.closeQuietly(outputStream); return new Image(base64Data,w,h); }catch(Exception ex){ throw new ReportComputeException(ex); } }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height) throws WriterException { return encode(contents, format, width, height, null); }
/** * 加密后的 * @param pd * @return Base64Code String */ public String createDESQrcode(PageData pd){ String Imgencode=""; byte[] imageByte=null; String formatName="PNG"; try { String encodeContent = pd.entrySet(). stream().filter(map -> map.getKey().equals("content")) .map(kv->DES.encrypt(String.valueOf(kv.getValue()), password)) .collect(Collectors.toList()) .get(0); //String encrypt = DES.encrypt(encodeContent, password); // int qrcodeWidth = 300; int qrcodeHeight = 300; HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(encodeContent, BarcodeFormat.QR_CODE, qrcodeWidth, qrcodeHeight, hints); ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix); ImageIO.write(bufferedImage, formatName, out); imageByte=out.toByteArray(); Imgencode = new BASE64Encoder().encode(imageByte); } catch (Exception e) { e.printStackTrace(); } return Imgencode; }
DecodeHandler(Looper looper, Context context) { super(looper); this.context = context; Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class); Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>(); decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS); decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS); decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS); hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats); hints.put(DecodeHintType.CHARACTER_SET, "ISO8859_1"); multiFormatReader = new MultiFormatReader(); multiFormatReader.setHints(hints); }
@Override public VINParsedResult parse(Result result) { if (result.getBarcodeFormat() != BarcodeFormat.CODE_39) { return null; } String rawText = result.getText(); rawText = IOQ.matcher(rawText).replaceAll("").trim(); if (!AZ09.matcher(rawText).matches()) { return null; } try { if (!checkChecksum(rawText)) { return null; } String wmi = rawText.substring(0, 3); return new VINParsedResult(rawText, wmi, rawText.substring(3, 9), rawText.substring(9, 17), countryCode(wmi), rawText.substring(3, 8), modelYear(rawText.charAt(9)), rawText.charAt(10), rawText.substring(11)); } catch (IllegalArgumentException iae) { return null; } }
private void launchSearch(String text) { Intent intent = new Intent(Intents.Encode.ACTION); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); intent.putExtra(Intents.Encode.TYPE, Contents.Type.TEXT); intent.putExtra(Intents.Encode.DATA, text); intent.putExtra(Intents.Encode.FORMAT, BarcodeFormat.QR_CODE.toString()); startActivity(intent); }
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException { if (format == BarcodeFormat.EAN_13) { return super.encode(contents, format, width, height, hints); } throw new IllegalArgumentException("Can only encode EAN_13, but got " + format); }
public final Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException { DecoderResult decoderResult; ResultPoint[] points; if (hints == null || !hints.containsKey(DecodeHintType.PURE_BARCODE)) { DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect(hints); decoderResult = this.decoder.decode(detectorResult.getBits(), (Map) hints); points = detectorResult.getPoints(); } else { decoderResult = this.decoder.decode(extractPureBits(image.getBlackMatrix()), (Map) hints); points = NO_POINTS; } if (decoderResult.getOther() instanceof QRCodeDecoderMetaData) { ((QRCodeDecoderMetaData) decoderResult.getOther()).applyMirroredCorrection(points); } Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE); List<byte[]> byteSegments = decoderResult.getByteSegments(); if (byteSegments != null) { result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, byteSegments); } String ecLevel = decoderResult.getECLevel(); if (ecLevel != null) { result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel); } if (decoderResult.hasStructuredAppend()) { result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE, Integer.valueOf (decoderResult.getStructuredAppendSequenceNumber())); result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_PARITY, Integer.valueOf (decoderResult.getStructuredAppendParity())); } return result; }
public DecodeThread(CaptureActivity activity, int decodeMode) { this.activity = activity; handlerInitLatch = new CountDownLatch(1); hints = new EnumMap<DecodeHintType, Object>(DecodeHintType.class); Collection<BarcodeFormat> decodeFormats = new ArrayList<BarcodeFormat>(); decodeFormats.addAll(EnumSet.of(BarcodeFormat.AZTEC)); decodeFormats.addAll(EnumSet.of(BarcodeFormat.PDF_417)); switch (decodeMode) { case BARCODE_MODE: decodeFormats.addAll(DecodeFormatManager.getBarCodeFormats()); break; case QRCODE_MODE: decodeFormats.addAll(DecodeFormatManager.getQrCodeFormats()); break; case ALL_MODE: decodeFormats.addAll(DecodeFormatManager.getBarCodeFormats()); decodeFormats.addAll(DecodeFormatManager.getQrCodeFormats()); break; default: break; } hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats); }
static public Bitmap generateBitmap(@NonNull String contentsToEncode, int imageWidth, int imageHeight, int marginSize) throws WriterException { Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); if (marginSize != MARGIN_AUTOMATIC) { // We want to generate with a custom margin size hints.put(EncodeHintType.MARGIN, marginSize); } MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result = writer .encode(contentsToEncode, BarcodeFormat.QR_CODE, imageWidth, imageHeight, hints); final int width = result.getWidth(); final 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) ? colorBack : colorWhite; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
public static Vector<BarcodeFormat> parseDecodeFormats(Intent intent) { List<String> scanFormats = null; String scanFormatsString = intent.getStringExtra(Intents.Scan.SCAN_FORMATS); if (scanFormatsString != null) { scanFormats = Arrays.asList(COMMA_PATTERN.split(scanFormatsString)); } return parseDecodeFormats(scanFormats, intent.getStringExtra(Intents.Scan.MODE)); }