Search in sources :

Example 1 with ParsedResult

use of com.google.zxing.client.result.ParsedResult in project zxing by zxing.

the class CaptureActivity method handleResult.

private void handleResult(Result result) {
    ParsedResult parsed = ResultParser.parseResult(result);
    Intent intent;
    if (parsed.getType() == ParsedResultType.URI) {
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse(((URIParsedResult) parsed).getURI()));
    } else {
        intent = new Intent(Intent.ACTION_WEB_SEARCH);
        intent.putExtra("query", ((TextParsedResult) parsed).getText());
    }
    startActivity(intent);
}
Also used : ParsedResult(com.google.zxing.client.result.ParsedResult) TextParsedResult(com.google.zxing.client.result.TextParsedResult) URIParsedResult(com.google.zxing.client.result.URIParsedResult) Intent(android.content.Intent)

Example 2 with ParsedResult

use of com.google.zxing.client.result.ParsedResult in project zxing by zxing.

the class ResultHandler method fillInCustomSearchURL.

final String fillInCustomSearchURL(String text) {
    if (customProductSearch == null) {
        // ?
        return text;
    }
    try {
        text = URLEncoder.encode(text, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    // can't happen; UTF-8 is always supported. Continue, I guess, without encoding      
    }
    String url = customProductSearch;
    if (rawResult != null) {
        // Replace %f but only if it doesn't seem to be a hex escape sequence. This remains
        // problematic but avoids the more surprising problem of breaking escapes
        url = url.replaceFirst("%f(?![0-9a-f])", rawResult.getBarcodeFormat().toString());
        if (url.contains("%t")) {
            ParsedResult parsedResultAgain = ResultParser.parseResult(rawResult);
            url = url.replace("%t", parsedResultAgain.getType().toString());
        }
    }
    // Replace %s last as it might contain itself %f or %t
    return url.replace("%s", text);
}
Also used : ParsedResult(com.google.zxing.client.result.ParsedResult) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with ParsedResult

use of com.google.zxing.client.result.ParsedResult in project zxing by zxing.

the class RSSExpandedImage2resultTestCase method assertCorrectImage2result.

private static void assertCorrectImage2result(String fileName, ExpandedProductParsedResult expected) throws IOException, NotFoundException {
    Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
    BufferedImage image = ImageIO.read(path.toFile());
    BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
    int rowNumber = binaryMap.getHeight() / 2;
    BitArray row = binaryMap.getBlackRow(rowNumber, null);
    Result theResult;
    try {
        RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
        theResult = rssExpandedReader.decodeRow(rowNumber, row, null);
    } catch (ReaderException re) {
        fail(re.toString());
        return;
    }
    assertSame(BarcodeFormat.RSS_EXPANDED, theResult.getBarcodeFormat());
    ParsedResult result = ResultParser.parseResult(theResult);
    assertEquals(expected, result);
}
Also used : Path(java.nio.file.Path) GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) ParsedResult(com.google.zxing.client.result.ParsedResult) ExpandedProductParsedResult(com.google.zxing.client.result.ExpandedProductParsedResult) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) ParsedResult(com.google.zxing.client.result.ParsedResult) ExpandedProductParsedResult(com.google.zxing.client.result.ExpandedProductParsedResult) ReaderException(com.google.zxing.ReaderException)

Example 4 with ParsedResult

use of com.google.zxing.client.result.ParsedResult in project weex-example by KalicyZhou.

the class QRCodeEncoder method encodeFromStreamExtra.

// Handles send intents from the Contacts app, retrieving a contact as a VCARD.
private void encodeFromStreamExtra(Intent intent) throws WriterException {
    format = BarcodeFormat.QR_CODE;
    Bundle bundle = intent.getExtras();
    if (bundle == null) {
        throw new WriterException("No extras");
    }
    Uri uri = bundle.getParcelable(Intent.EXTRA_STREAM);
    if (uri == null) {
        throw new WriterException("No EXTRA_STREAM");
    }
    byte[] vcard;
    String vcardString;
    InputStream stream = null;
    try {
        stream = activity.getContentResolver().openInputStream(uri);
        if (stream == null) {
            throw new WriterException("Can't open stream for " + uri);
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[2048];
        int bytesRead;
        while ((bytesRead = stream.read(buffer)) > 0) {
            baos.write(buffer, 0, bytesRead);
        }
        vcard = baos.toByteArray();
        vcardString = new String(vcard, 0, vcard.length, "UTF-8");
    } catch (IOException ioe) {
        throw new WriterException(ioe);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            // continue
            }
        }
    }
    Log.d(TAG, "Encoding share intent content:");
    Log.d(TAG, vcardString);
    Result result = new Result(vcardString, vcard, null, BarcodeFormat.QR_CODE);
    ParsedResult parsedResult = ResultParser.parseResult(result);
    if (!(parsedResult instanceof AddressBookParsedResult)) {
        throw new WriterException("Result was not an address");
    }
    encodeQRCodeContents((AddressBookParsedResult) parsedResult);
    if (contents == null || contents.isEmpty()) {
        throw new WriterException("No content to encode");
    }
}
Also used : AddressBookParsedResult(com.google.zxing.client.result.AddressBookParsedResult) Bundle(android.os.Bundle) InputStream(java.io.InputStream) ParsedResult(com.google.zxing.client.result.ParsedResult) AddressBookParsedResult(com.google.zxing.client.result.AddressBookParsedResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Uri(android.net.Uri) WriterException(com.google.zxing.WriterException) Result(com.google.zxing.Result) ParsedResult(com.google.zxing.client.result.ParsedResult) AddressBookParsedResult(com.google.zxing.client.result.AddressBookParsedResult)

Example 5 with ParsedResult

use of com.google.zxing.client.result.ParsedResult in project keepass2android by PhilippC.

the class QRCodeEncoder method encodeFromStreamExtra.

// Handles send intents from the Contacts app, retrieving a contact as a VCARD.
private void encodeFromStreamExtra(Intent intent) throws WriterException {
    format = BarcodeFormat.QR_CODE;
    Bundle bundle = intent.getExtras();
    if (bundle == null) {
        throw new WriterException("No extras");
    }
    Uri uri = bundle.getParcelable(Intent.EXTRA_STREAM);
    if (uri == null) {
        throw new WriterException("No EXTRA_STREAM");
    }
    byte[] vcard;
    String vcardString;
    try {
        InputStream stream = activity.getContentResolver().openInputStream(uri);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[2048];
        int bytesRead;
        while ((bytesRead = stream.read(buffer)) > 0) {
            baos.write(buffer, 0, bytesRead);
        }
        vcard = baos.toByteArray();
        vcardString = new String(vcard, 0, vcard.length, "UTF-8");
    } catch (IOException ioe) {
        throw new WriterException(ioe);
    }
    Log.d(TAG, "Encoding share intent content:");
    Log.d(TAG, vcardString);
    Result result = new Result(vcardString, vcard, null, BarcodeFormat.QR_CODE);
    ParsedResult parsedResult = ResultParser.parseResult(result);
    if (!(parsedResult instanceof AddressBookParsedResult)) {
        throw new WriterException("Result was not an address");
    }
    encodeQRCodeContents((AddressBookParsedResult) parsedResult);
    if (contents == null || contents.isEmpty()) {
        throw new WriterException("No content to encode");
    }
}
Also used : AddressBookParsedResult(com.google.zxing.client.result.AddressBookParsedResult) Bundle(android.os.Bundle) InputStream(java.io.InputStream) ParsedResult(com.google.zxing.client.result.ParsedResult) AddressBookParsedResult(com.google.zxing.client.result.AddressBookParsedResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Uri(android.net.Uri) WriterException(com.google.zxing.WriterException) Result(com.google.zxing.Result) ParsedResult(com.google.zxing.client.result.ParsedResult) AddressBookParsedResult(com.google.zxing.client.result.AddressBookParsedResult)

Aggregations

ParsedResult (com.google.zxing.client.result.ParsedResult)18 Result (com.google.zxing.Result)8 Uri (android.net.Uri)6 Bundle (android.os.Bundle)6 WriterException (com.google.zxing.WriterException)6 AddressBookParsedResult (com.google.zxing.client.result.AddressBookParsedResult)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Intent (android.content.Intent)4 CardPresenter (com.github.barcodeeye.scan.api.CardPresenter)3 ArrayList (java.util.ArrayList)3 BinaryBitmap (com.google.zxing.BinaryBitmap)2 BufferedImage (java.awt.image.BufferedImage)2 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)1 LuminanceSource (com.google.zxing.LuminanceSource)1 MultiFormatReader (com.google.zxing.MultiFormatReader)1 NotFoundException (com.google.zxing.NotFoundException)1 ReaderException (com.google.zxing.ReaderException)1