下面列出了com.google.zxing.Result#getText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Handler scan result
*
* @param result
* @param barcode
*/
public void handleDecode(Result result, Bitmap barcode) {
inactivityTimer.onActivity();
playBeepSoundAndVibrate();
String resultString = result.getText();
//FIXME
if (TextUtils.isEmpty(resultString)) {
Toast.makeText(CaptureActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show();
} else {
Intent resultIntent = new Intent();
Bundle bundle = new Bundle();
bundle.putString(Constant.INTENT_EXTRA_KEY_QR_SCAN, resultString);
System.out.println("sssssssssssssssss scan 0 = " + resultString);
// 不能使用Intent传递大于40kb的bitmap,可以使用一个单例对象存储这个bitmap
// bundle.putParcelable("bitmap", barcode);
// Logger.d("saomiao",resultString);
resultIntent.putExtras(bundle);
this.setResult(RESULT_OK, resultIntent);
}
CaptureActivity.this.finish();
}
@Override
public void handleResult(Result result) {
String mUserName = "";
String mLoginToken = "";
String s = result.getText();
if (!TextUtils.isEmpty(s) && s.contains(",")) {
String[] data = s.split(",", 2);
if (data.length == 2) {
mUserName = data[0];
mLoginToken = data[1];
}
}
JLog.d("Login info: ", "UserName === " + mUserName + " LoginToken ======= "+ mLoginToken);
mPresenter.login(this, mUserName, mLoginToken);
}
/**
* 二维码解码
*
* @param imgPath
* @return String
*/
public static String decode2(String imgPath) {
BufferedImage image = null;
Result result = null;
try {
image = ImageIO.read(new File(imgPath));
if (image == null) {
System.out.println("the decode image may be not exit.");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "GBK");
result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public URIParsedResult parse(Result result)
{
String s = result.getText();
if (s.startsWith("URL:"))
{
s = s.substring(4);
}
String s1 = s.trim();
if (a(s1))
{
return new URIParsedResult(s1, null);
} else
{
return null;
}
}
/**
* Handler scan result
*
* @param result
* @param barcode
*/
public void handleDecode(Result result, Bitmap barcode) {
inactivityTimer.onActivity();
playBeepSoundAndVibrate();
String resultString = result.getText();
if (TextUtils.isEmpty(resultString)) {
Toast.makeText(CaptureActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show();
} else {
Intent resultIntent = new Intent();
Bundle bundle = new Bundle();
bundle.putString(INTENT_EXTRA_KEY_QR_SCAN, resultString);
resultIntent.putExtras(bundle);
this.setResult(RESULT_OK, resultIntent);
}
CaptureActivity.this.finish();
}
/**
* Handler scan result
*
* @param result
* @param barcode
*/
public void handleDecode(Result result, Bitmap barcode) {
inactivityTimer.onActivity();
playBeepSoundAndVibrate();
String resultString = result.getText();
//FIXME
if (TextUtils.isEmpty(resultString)) {
Toast.makeText(CaptureActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show();
} else {
Intent resultIntent = new Intent();
Bundle bundle = new Bundle();
bundle.putString(Constant.INTENT_EXTRA_KEY_QR_SCAN, resultString);
System.out.println("sssssssssssssssss scan 0 = " + resultString);
// 不能使用Intent传递大于40kb的bitmap,可以使用一个单例对象存储这个bitmap
// bundle.putParcelable("bitmap", barcode);
// Logger.d("saomiao",resultString);
resultIntent.putExtras(bundle);
this.setResult(RESULT_OK, resultIntent);
}
CaptureActivity.this.finish();
}
private static Result translateResultPoints(Result result, int xOffset, int yOffset) {
ResultPoint[] oldResultPoints = result.getResultPoints();
if (oldResultPoints == null) {
return result;
}
ResultPoint[] newResultPoints = new ResultPoint[oldResultPoints.length];
for (int i = 0; i < oldResultPoints.length; i++) {
ResultPoint oldPoint = oldResultPoints[i];
if (oldPoint != null) {
newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
}
}
Result newResult = new Result(result.getText(),
result.getRawBytes(),
result.getNumBits(),
newResultPoints,
result.getBarcodeFormat(),
result.getTimestamp());
newResult.putAllMetadata(result.getResultMetadata());
return newResult;
}
public String decodeWithZxing(Bitmap bitmap) {
MultiFormatReader multiFormatReader = new MultiFormatReader();
multiFormatReader.setHints(changeZXingDecodeDataMode());
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
Result rawResult = null;
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
if (source != null) {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
rawResult = multiFormatReader.decodeWithState(binaryBitmap);
} catch (ReaderException re) {
// continue
} finally {
multiFormatReader.reset();
}
}
return rawResult != null ? rawResult.getText() : null;
}
/**
* Handler scan result
*
* @param result
* @param barcode 获取结果
*/
public void handleDecode(Result result, Bitmap barcode) {
inactivityTimer.onActivity();
playBeepSoundAndVibrate();
String resultString = result.getText();
// FIXME
if (resultString.equals("")) {
Toast.makeText(CaptureActivity.this, "没有扫描到任何东西!", Toast.LENGTH_SHORT)
.show();
} else {
if (backResult) {
Intent resultIntent = new Intent();
Bundle bundle = new Bundle();
bundle.putString(SCAN_RESULT_KEY, resultString);
resultIntent.putExtras(bundle);
this.setResult(RESULT_OK, resultIntent);
} else {
sendResultToWebLogin(resultString);
}
}
CaptureActivity.this.finish();
}
protected static String getMassagedText(Result result) {
String text = result.getText();
if (text.startsWith(BYTE_ORDER_MARK)) {
text = text.substring(1);
}
return text;
}
public static ParsedResult parseResult(Result theResult) {
for (ResultParser parser : PARSERS) {
ParsedResult result = parser.parse(theResult);
if (result != null) {
return result;
}
}
return new TextParsedResult(theResult.getText(), null);
}
private static Result maybeReturnResult(Result result) throws FormatException {
String text = result.getText();
if (text.charAt(0) == '0') {
return new Result(text.substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
} else {
throw FormatException.getFormatInstance();
}
}
private static Result translateResultPoints(Result result, int xOffset, int yOffset) {
ResultPoint[] oldResultPoints = result.getResultPoints();
if (oldResultPoints == null) {
return result;
}
ResultPoint[] newResultPoints = new ResultPoint[oldResultPoints.length];
for (int i = 0; i < oldResultPoints.length; i++) {
ResultPoint oldPoint = oldResultPoints[i];
newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
}
Result newResult = new Result(result.getText(), result.getRawBytes(), newResultPoints, result.getBarcodeFormat());
newResult.putAllMetadata(result.getResultMetadata());
return newResult;
}
protected static String getMassagedText(Result result) {
String text = result.getText();
if (text.startsWith(BYTE_ORDER_MARK)) {
text = text.substring(1);
}
return text;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View layout;
if (view instanceof LinearLayout) {
layout = view;
} else {
LayoutInflater factory = LayoutInflater.from(activity);
layout = factory.inflate(R.layout.history_list_item, viewGroup, false);
}
HistoryItem item = getItem(position);
Result result = item.getResult();
CharSequence title;
CharSequence detail;
if (result != null) {
title = result.getText();
detail = item.getDisplayAndDetails();
} else {
Resources resources = getContext().getResources();
title = resources.getString(R.string.history_empty);
detail = resources.getString(R.string.history_empty_detail);
}
((TextView) layout.findViewById(R.id.history_title)).setText(title);
((TextView) layout.findViewById(R.id.history_detail)).setText(detail);
return layout;
}
@Override
public URIParsedResult parse(Result result) {
String rawText = result.getText();
if (!rawText.startsWith("MEBKM:")) {
return null;
}
String title = matchSingleDoCoMoPrefixedField("TITLE:", rawText, true);
String[] rawUri = matchDoCoMoPrefixedField("URL:", rawText, true);
if (rawUri == null) {
return null;
}
String uri = rawUri[0];
return URIResultParser.isBasicallyValidURI(uri) ? new URIParsedResult(uri, title) : null;
}
/**
* 解析QRCode二维码
*/
@SuppressWarnings("unchecked")
public static void decode(File file) {
try {
BufferedImage image;
try {
image = ImageIO.read(file);
if (image == null) {
System.out.println("Could not decode image");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
@SuppressWarnings("rawtypes")
Hashtable hints = new Hashtable();
//解码设置编码方式为:utf-8
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
System.out.println("解析后内容:" + resultStr);
} catch (IOException ioe) {
System.out.println(ioe.toString());
} catch (ReaderException re) {
System.out.println(re.toString());
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
private void initDialogResult(Result result) {
BarcodeFormat type = result.getBarcodeFormat();
String realContent = result.getText();
if (rxDialogSure == null) {
rxDialogSure = new RxDialogSure(mContext);//提示弹窗
}
if (BarcodeFormat.QR_CODE.equals(type)) {
rxDialogSure.setTitle("二维码扫描结果");
} else if (BarcodeFormat.EAN_13.equals(type)) {
rxDialogSure.setTitle("条形码扫描结果");
} else {
rxDialogSure.setTitle("扫描结果");
}
rxDialogSure.setContent(realContent);
rxDialogSure.setSureListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rxDialogSure.cancel();
}
});
rxDialogSure.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (handler != null) {
// 连续扫描,不发送此消息扫描一次结束后就不能再次扫描
handler.sendEmptyMessage(R.id.restart_preview);
}
}
});
if (!rxDialogSure.isShowing()) {
rxDialogSure.show();
}
RxSPTool.putContent(mContext, RxConstants.SP_SCAN_CODE, RxDataTool.stringToInt(RxSPTool.getContent(mContext, RxConstants.SP_SCAN_CODE)) + 1 + "");
}
private static Result maybeReturnResult(Result result) throws FormatException {
String text = result.getText();
if (text.charAt(0) == '0') {
return new Result(text.substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
} else {
throw FormatException.getFormatInstance();
}
}
@Override
public URIParsedResult parse(Result result) {
String rawText = result.getText();
if (!rawText.startsWith("MEBKM:")) {
return null;
}
String title = matchSingleDoCoMoPrefixedField("TITLE:", rawText, true);
String[] rawUri = matchDoCoMoPrefixedField("URL:", rawText, true);
if (rawUri == null) {
return null;
}
String uri = rawUri[0];
return URIResultParser.isBasicallyValidURI(uri) ? new URIParsedResult(uri, title) : null;
}