本文整理匯總了Java中com.google.zxing.qrcode.QRCodeReader.decode方法的典型用法代碼示例。如果您正苦於以下問題:Java QRCodeReader.decode方法的具體用法?Java QRCodeReader.decode怎麽用?Java QRCodeReader.decode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.zxing.qrcode.QRCodeReader
的用法示例。
在下文中一共展示了QRCodeReader.decode方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: scanningImage
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
protected Result scanningImage(Uri path) {
if (path == null || path.equals("")) {
return null;
}
// DecodeHintType 和EncodeHintType
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 設置二維碼內容的編碼
try {
Bitmap scanBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
return reader.decode(bitmap1, hints);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例2: readQrCode
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
/**
* 讀取二維碼
* @param qrCodeFile
* @return
*/
public String readQrCode(File qrCodeFile){
String ret = null;
try {
QRCodeReader reader = new QRCodeReader();
BufferedImage image = ImageIO.read(qrCodeFile);
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap imageBinaryBitmap = new BinaryBitmap(binarizer);
Result result = reader.decode(imageBinaryBitmap);
ret = result.getText();
} catch (IOException |NotFoundException | ChecksumException | FormatException e) {
Exceptions.printException(e);
}
return ret;
}
示例3: scanningImage
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
/**
* 掃描二維碼圖片的方法
*
* @param path
* @return
*/
public Result scanningImage(String path) {
try {
if (TextUtils.isEmpty(path)) {
return null;
}
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); // 設置二維碼內容的編碼
InputStream is = new FileInputStream(path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
// width,hight設為原來的十分一
options.inSampleSize = 10;
scanBitmap = BitmapFactory.decodeStream(is, null, options);
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
return reader.decode(bitmap, hints);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例4: decode
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
/**
* Decodes a QR code from a BufferedImage object.
*
* @param image
* @return a Result object containing the decoded data or information about
* an unsuccessful decoding attempt
* @throws Exception
*/
private Result decode(BufferedImage image) throws Exception {
// create a luminance source from the BufferedImage
LuminanceSource lumSource = new BufferedImageLuminanceSource(image);
// create a binary bitmap from the luminance source. a Binarizer
// converts luminance data to 1 bit data.
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(lumSource));
// a reader for decoding
QRCodeReader reader = new QRCodeReader();
// attempt decoding and return result
Hashtable<DecodeHintType, Boolean> hints = new Hashtable<DecodeHintType, Boolean>();
hints.put(DecodeHintType.TRY_HARDER, true);
return reader.decode(bitmap, hints);
}
示例5: decode
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
public static String decode(BufferedImage image) {
// convert the image to a binary bitmap source
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
// decode the barcode
QRCodeReader reader = new QRCodeReader();
try {
@SuppressWarnings("rawtypes")
Hashtable hints = new Hashtable();
Result result = reader.decode(bitmap, hints);
return result.getText();
} catch (ReaderException e) {
// the data is improperly formatted
}
return "";
}
示例6: decode
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
private String decode(byte[] data, int width, int height) {
ScannerManager manager = mManager.get();
if (manager == null) {
return null;
}
Rect rect = manager.getFramingRectInPreview();
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data,
width, height, rect.left, rect.top, rect.right, rect.bottom, false);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
try {
Result result = reader.decode(bitmap, mHints);
return result.getText();
} catch (ReaderException e) {
// Ignore as we will repeatedly decode the preview frame
return null;
}
}
示例7: readQRCode
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
/**
* Versucht aus dem �bergeben BufferedImage ein QR Code zu finden und �bersetzt dieses in einen String.
*
* @param qrcodeImage : BufferedImage
* @return String mit dem Inhalt des QRCodes
* @throws Exception
*/
private static String readQRCode(BufferedImage qrcodeImage) throws Exception{
//Die Parameter anlegen
Hashtable<DecodeHintType, Object> hintMap = new Hashtable<DecodeHintType, Object>();
hintMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
//Bild zu BinaryBitmap verwandeln
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(qrcodeImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
//QR Leser initialisieren...
QRCodeReader reader = new QRCodeReader();
Result result;
//...und lesen:
result = reader.decode(bitmap,hintMap);
return result.getText();
}
示例8: decode
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
public String decode(BufferedImage image) {
// convert the image to a binary bitmap source
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
// decode the barcode
QRCodeReader reader = new QRCodeReader();
try {
@SuppressWarnings("rawtypes")
Hashtable hints = new Hashtable();
Result result = reader.decode(bitmap, hints);
log.info("Decoded image successfully, result was : '" + result.getText() + "'");
return result.getText();
} catch (ReaderException e) {
// the data is improperly formatted
log.debug(e.getMessage());
log.error("Error while decoding image", e);
}
return "";
}
示例9: decodeImage
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
/**
* Decode the data within the viewfinder rectangle, and time how long it took. For efficiency, reuse the same reader
* objects from one decode to the next.
*/
public static Result decodeImage(byte[] data, int width, int height) {
// 處理
Result result = null;
try {
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
PlanarYUVLuminanceSource source =
new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false);
/**
* HybridBinarizer算法使用了更高級的算法,但使用GlobalHistogramBinarizer識別效率確實比HybridBinarizer要高一些。
*
* GlobalHistogram算法:(http://kuangjianwei.blog.163.com/blog/static/190088953201361015055110/)
*
* 二值化的關鍵就是定義出黑白的界限,我們的圖像已經轉化為了灰度圖像,每個點都是由一個灰度值來表示,就需要定義出一個灰度值,大於這個值就為白(0),低於這個值就為黑(1)。
* 在GlobalHistogramBinarizer中,是從圖像中均勻取5行(覆蓋整個圖像高度),每行取中間五分之四作為樣本;以灰度值為X軸,每個灰度值的像素個數為Y軸建立一個直方圖,
* 從直方圖中取點數最多的一個灰度值,然後再去給其他的灰度值進行分數計算,按照點數乘以與最多點數灰度值的距離的平方來進行打分,選分數最高的一個灰度值。接下來在這兩個灰度值中間選取一個區分界限,
* 取的原則是盡量靠近中間並且要點數越少越好。界限有了以後就容易了,與整幅圖像的每個點進行比較,如果灰度值比界限小的就是黑,在新的矩陣中將該點置1,其餘的就是白,為0。
*/
BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source));
// BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader2 = new QRCodeReader();
result = reader2.decode(bitmap1, hints);
} catch (ReaderException e) {
}
return result;
}
示例10: decodeImage
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
/**
* Decode the data within the viewfinder rectangle, and time how long it took. For efficiency, reuse the same reader
* objects from one decode to the next.
*/
public static Result decodeImage(byte[] data, int width, int height) {
// 處理
Result result = null;
try {
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
PlanarYUVLuminanceSource source =
new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false);
/**
* HybridBinarizer算法使用了更高級的算法,但使用GlobalHistogramBinarizer識別效率確實比HybridBinarizer要高一些。
*
* GlobalHistogram算法:(http://kuangjianwei.blog.163.com/blog/static/190088953201361015055110/)
*
* 二值化的關鍵就是定義出黑白的界限,我們的圖像已經轉化為了灰度圖像,每個點都是由一個灰度值來表示,就需要定義出一個灰度值,大於這個值就為白(0),低於這個值就為黑(1)。
* 在GlobalHistogramBinarizer中,是從圖像中均勻取5行(覆蓋整個圖像高度),每行取中間五分之四作為樣本;以灰度值為X軸,每個灰度值的像素個數為Y軸建立一個直方圖,
* 從直方圖中取點數最多的一個灰度值,然後再去給其他的灰度值進行分數計算,按照點數乘以與最多點數灰度值的距離的平方來進行打分,選分數最高的一個灰度值。接下來在這兩個灰度值中間選取一個區分界限,
* 取的原則是盡量靠近中間並且要點數越少越好。界限有了以後就容易了,與整幅圖像的每個點進行比較,如果灰度值比界限小的就是黑,在新的矩陣中將該點置1,其餘的就是白,為0。
*/
BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source));
// BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader2 = new QRCodeReader();
result = reader2.decode(bitmap1, hints);
} catch (ReaderException e) {
}
return result;
}
示例11: scanningImage
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
protected Result scanningImage(String path) {
if (StringUtil.isBlank(path)) {
return null;
}
// DecodeHintType 和EncodeHintType
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 設置二維碼內容的編碼
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 先獲取原大小
scanBitmap = BitmapFactory.decodeFile(path, options);
options.inJustDecodeBounds = false; // 獲取新的大小
int sampleSize = (int) (options.outHeight / (float) 200);
if (sampleSize <= 0)
sampleSize = 1;
options.inSampleSize = sampleSize;
scanBitmap = BitmapFactory.decodeFile(path, options);
int width = scanBitmap.getWidth(), height = scanBitmap.getHeight();
int[] pixels = new int[width * height];
scanBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
scanBitmap.recycle();
scanBitmap = null;
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
try {
return reader.decode(bitmap1, hints);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例12: decodeQRCodeImage
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
/**
* 解碼 QRCode 圖片,解析出其內容
*
* @param imageURI QRCode 圖片 URI
* @return 解析後的內容
* @throws IOException
*/
public static String decodeQRCodeImage(URI imageURI) throws IOException {
BufferedImage bufferedImage = ImageReader.readImage(imageURI);
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
try {
Result result = reader.decode(bitmap);
return result.getText();
} catch (ReaderException e) {
e.printStackTrace();
}
return "";
}
示例13: getStringFromQRCode
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
public static String getStringFromQRCode(String path) {
String httpString = null;
Bitmap bmp = convertToBitmap(path);
byte[] data = getYUV420sp(bmp.getWidth(), bmp.getHeight(), bmp);
// 處理
try {
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
// hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data,
bmp.getWidth(),
bmp.getHeight(),
0, 0,
bmp.getWidth(),
bmp.getHeight(),
false);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader2= new QRCodeReader();
Result result = reader2.decode(bitmap1, hints);
httpString = result.getText();
} catch (Exception e) {
e.printStackTrace();
}
bmp.recycle();
bmp = null;
return httpString;
}
示例14: decodePureBitmap
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
public static Result decodePureBitmap(Bitmap bmp) throws FormatException, ChecksumException, NotFoundException {
int width = bmp.getWidth(), height = bmp.getHeight();
int[] pixels = new int[width * height];
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
QRCodeReader reader = new QRCodeReader();
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
return reader.decode(binaryBitmap, hints);
}
示例15: doInBackground
import com.google.zxing.qrcode.QRCodeReader; //導入方法依賴的package包/類
@Override
protected Result doInBackground(Uri... params) {
if (params == null || params.length != 1) {
return null;
}
try {
InputStream inputStream = QRScanActivity.this.getContentResolver().openInputStream(params[0]);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Rect padding = new Rect(0, 0, 0, 0);
BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
int minLength = Math.min(options.outWidth, options.outHeight);
int MAX = 512; // 圖片短邊不超過 512
if (minLength > MAX) {
options.inSampleSize = minLength / MAX;
}
options.inJustDecodeBounds = false;
// 流打開後隻能用一次, 需要重新獲取
inputStream = QRScanActivity.this.getContentResolver().openInputStream(params[0]);
// 對圖片裁剪後再掃碼, 否則花的時間太長
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
if (bitmap == null) {
return null;
}
int width = bitmap.getWidth(), height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
bitmap.recycle();
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
Result result = reader.decode(bBitmap);
return result;
} catch (Exception e) {
Log.e(TAG, "can not open file", e);
return null;
}
}