Search in sources :

Example 96 with Result

use of com.google.zxing.Result in project QRCode by 5peak2me.

the class CaptureActivity method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (resultCode == RESULT_OK) {
        final ProgressDialog progressDialog;
        switch(requestCode) {
            case REQUEST_CODE:
                // 获取选中图片的路径
                Cursor cursor = getContentResolver().query(intent.getData(), null, null, null, null);
                if (cursor.moveToFirst()) {
                    photoPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                }
                cursor.close();
                progressDialog = new ProgressDialog(this);
                progressDialog.setMessage("正在扫描...");
                progressDialog.setCancelable(false);
                progressDialog.show();
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        Bitmap img = BitmapUtils.getCompressedBitmap(photoPath);
                        BitmapDecoder decoder = new BitmapDecoder(CaptureActivity.this);
                        Result result = decoder.getRawResult(img);
                        if (result != null) {
                            Message m = mHandler.obtainMessage();
                            m.what = PARSE_BARCODE_SUC;
                            m.obj = ResultParser.parseResult(result).toString();
                            mHandler.sendMessage(m);
                        } else {
                            Message m = mHandler.obtainMessage();
                            m.what = PARSE_BARCODE_FAIL;
                            mHandler.sendMessage(m);
                        }
                        progressDialog.dismiss();
                    }
                }).start();
                break;
        }
    }
}
Also used : BitmapDecoder(com.google.zxing.decode.BitmapDecoder) Bitmap(android.graphics.Bitmap) Message(android.os.Message) ProgressDialog(android.app.ProgressDialog) Cursor(android.database.Cursor) Result(com.google.zxing.Result)

Example 97 with Result

use of com.google.zxing.Result in project QRCode by 5peak2me.

the class DecodeHandler method decode.

/**
	 * 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.
	 * 
	 * @param data
	 *            The YUV preview frame.
	 * @param width
	 *            The width of the preview frame.
	 * @param height
	 *            The height of the preview frame.
	 */
private void decode(byte[] data, int width, int height) {
    Size size = activity.getCameraManager().getPreviewSize();
    // 这里需要将获取的data翻转一下,因为相机默认拿的的横屏的数据
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < size.height; y++) {
        for (int x = 0; x < size.width; x++) rotatedData[x * size.height + size.height - y - 1] = data[x + y * size.width];
    }
    // 宽高也要调整
    int tmp = size.width;
    size.width = size.height;
    size.height = tmp;
    Result rawResult = null;
    PlanarYUVLuminanceSource source = buildLuminanceSource(rotatedData, size.width, size.height);
    if (source != null) {
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = multiFormatReader.decodeWithState(bitmap);
        } catch (ReaderException re) {
        // continue
        } finally {
            multiFormatReader.reset();
        }
    }
    Handler handler = activity.getHandler();
    if (rawResult != null) {
        // Don't log the barcode contents for security.
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
            Bundle bundle = new Bundle();
            bundleThumbnail(source, bundle);
            message.setData(bundle);
            message.sendToTarget();
        }
    } else {
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_failed);
            message.sendToTarget();
        }
    }
}
Also used : Message(android.os.Message) Size(android.hardware.Camera.Size) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) Bundle(android.os.Bundle) Handler(android.os.Handler) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 98 with Result

use of com.google.zxing.Result in project zxing by zxing.

the class DecodeHandler method decode.

/**
   * 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.
   *
   * @param data   The YUV preview frame.
   * @param width  The width of the preview frame.
   * @param height The height of the preview frame.
   */
private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
    if (source != null) {
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = multiFormatReader.decodeWithState(bitmap);
        } catch (ReaderException re) {
        // continue
        } finally {
            multiFormatReader.reset();
        }
    }
    Handler handler = activity.getHandler();
    if (rawResult != null) {
        // Don't log the barcode contents for security.
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode in " + (end - start) + " ms");
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
            Bundle bundle = new Bundle();
            bundleThumbnail(source, bundle);
            message.setData(bundle);
            message.sendToTarget();
        }
    } else {
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_failed);
            message.sendToTarget();
        }
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) Bundle(android.os.Bundle) Handler(android.os.Handler) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 99 with Result

use of com.google.zxing.Result in project zxing by zxing.

the class HistoryManager method buildHistoryItem.

public HistoryItem buildHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(number + 1);
        String text = cursor.getString(0);
        String display = cursor.getString(1);
        String format = cursor.getString(2);
        long timestamp = cursor.getLong(3);
        String details = cursor.getString(4);
        Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
        return new HistoryItem(result, display, details);
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) Result(com.google.zxing.Result)

Example 100 with Result

use of com.google.zxing.Result in project zxing by zxing.

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)

Aggregations

Result (com.google.zxing.Result)116 ResultPoint (com.google.zxing.ResultPoint)43 BinaryBitmap (com.google.zxing.BinaryBitmap)36 ReaderException (com.google.zxing.ReaderException)35 HybridBinarizer (com.google.zxing.common.HybridBinarizer)29 Bundle (android.os.Bundle)19 Message (android.os.Message)17 DecoderResult (com.google.zxing.common.DecoderResult)14 ArrayList (java.util.ArrayList)14 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)11 MultiFormatReader (com.google.zxing.MultiFormatReader)10 Cursor (android.database.Cursor)9 NotFoundException (com.google.zxing.NotFoundException)9 Handler (android.os.Handler)8 LuminanceSource (com.google.zxing.LuminanceSource)8 BitArray (com.google.zxing.common.BitArray)8 DetectorResult (com.google.zxing.common.DetectorResult)8 BufferedImage (java.awt.image.BufferedImage)8 Test (org.junit.Test)8 Bitmap (android.graphics.Bitmap)7