本文整理匯總了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;
}