android zxing库为qr代码

时间:2016-04-14 11:12:37

标签: android zxing

当我扫描此条形码时

enter image description here

我得到了这个结果扫描

enter image description here

但我希望此内容结果以这种方式检测它是否包含SMS或电子邮件消息,例如,如果此内容包含电子邮件消息我需要打开电子邮件窗口,并且我想填写诸如To:这样的字段,使用此电子邮件,主题和身体。

我的代码到目前为止:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    String content = intent.getStringExtra("SCAN_RESULT");
}

2 个答案:

答案 0 :(得分:2)

您需要仔细检查电子邮件是否有多个,在此示例中我只考虑一封电子邮件。如果你看到email.getTos()方法,你会看到它是一个String []。

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

    Result res = new Result(scanResult.getContents(), null, null, BarcodeFormat.QR_CODE);
    ParsedResult parserdResult = ResultParser.parseResult(res);

    Log.d("GORIO", "TYPE: " + parserdResult.getType().toString());

    switch (parserdResult.getType()) {
        case ADDRESSBOOK:
            Log.d("GORIO", "ADDRESSBOOK: " + parserdResult.getDisplayResult());
            break;
        case EMAIL_ADDRESS:
            Log.d("GORIO", "EMAIL_ADDRESS: " + parserdResult.getDisplayResult());
            EmailAddressParsedResult email = (EmailAddressParsedResult) parserdResult;

            if (email.getTos().length > 0)
                Log.d("GORIO", "E-mail: " + email.getTos()[0] + " Subject: " + email.getSubject() + " Body: " + email.getBody());
            break;
        case PRODUCT:
            ProductParsedResult prod = (ProductParsedResult) parserdResult;
            Log.d("GORIO", "PRODUCT: " + scanResult.getContents());
            break;
        case URI:
            URIParsedResult uri = (URIParsedResult) parserdResult;
            Log.d("GORIO", "URI: " + scanResult.getContents());
            break;
        case GEO:
            GeoParsedResult geo = (GeoParsedResult) parserdResult;
            Log.d("GORIO", "GEO: " + scanResult.getContents());
            break;
        case TEL:
            TelParsedResult tel = (TelParsedResult) parserdResult;
            Log.d("GORIO", "TEL: " + scanResult.getContents());
            break;
        case SMS:
            Log.d("GORIO", "SMS: " + scanResult.getContents());
            SMSParsedResult sms = (SMSParsedResult) parserdResult;
            Log.d("GORIO", "SMS: " + sms.getNumbers());
            break;
        case CALENDAR:
            Log.d("GORIO", "CALENDAR: " + scanResult.getContents());
            break;
        case ISBN:
            ISBNParsedResult isbn = (ISBNParsedResult) parserdResult;
            Log.d("GORIO", "ISBN: " + scanResult.getContents());
            break;
        default:
    }
}

答案 1 :(得分:-1)

我扫描你的qrcode并得到这些原始文本:" MATMSG:TO:gresa@gmail.com; SUB:hello; BODY:hdfukfhdksu ;;"。

步骤1.您应解析您的架构获取EmailSendObject,如下所示:

   public static EmailSendObject parseQrcodeText(String rawText){
        //todo parse;
        EmailSendObject.recipent;//should be "gresa@gmail.com"
        EmailSendObject.subject;//should be "hello"
        EmailSendObject.cc;
        EmailSendObject.content;//should be "hdfukfhdksu;"
        return EmailSendObject;
    }

步骤2.通过android动作发送电子邮件:" ACTION_SENDTO"。

 public static void sendEmail(Activity activity,EmailSendObject object){
    Uri uri = Uri.parse("mailto:"+object.recipient);
    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
    intent.putExtra(Intent.EXTRA_CC, object.cc);
    intent.putExtra(Intent.EXTRA_SUBJECT, object.subject);
    intent.putExtra(Intent.EXTRA_TEXT, object.content);
    activity.startActivity(Intent.createChooser(intent, "Choose a email app!"));
}