本文整理汇总了Java中com.google.zxing.BarcodeFormat类的典型用法代码示例。如果您正苦于以下问题:Java BarcodeFormat类的具体用法?Java BarcodeFormat怎么用?Java BarcodeFormat使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BarcodeFormat类属于com.google.zxing包,在下文中一共展示了BarcodeFormat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: analyzeBitmap
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
/**
* 解析二维码图片工具类
*
* @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";
}
}
示例2: encode
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
/**
* 条形码编码
*/
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;
}
示例3: drawResultPoints
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
/**
* 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);
}
}
}
}
}
示例4: updateQrCode
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
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);
}
}
示例5: createQrcode
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
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;
}
示例6: endcode
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
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;
}
示例7: createQRCode
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
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;
}
示例8: encode
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
/**
* 生成条形二维码
*/
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;
}
示例9: CaptureActivityHandler
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
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();
}
示例10: parse
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
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);
}
示例11: encodeContentsFromZXingIntent
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
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;
}
示例12: createQRCode
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
/**
* 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;
}
示例13: create
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
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);
}
}
示例14: createBarCode
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
/**条形码的生成*/
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());
}
}
示例15: Create2DCode
import com.google.zxing.BarcodeFormat; //导入依赖的package包/类
/**
* 传入字符串生成二维码
*
* @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;
}