Search in sources :

Example 1 with Barcode

use of com.google.android.gms.vision.barcode.Barcode in project android-vision by googlesamples.

the class MainActivity method onActivityResult.

/**
     * Called when an activity you launched exits, giving you the requestCode
     * you started it with, the resultCode it returned, and any additional
     * data from it.  The <var>resultCode</var> will be
     * {@link #RESULT_CANCELED} if the activity explicitly returned that,
     * didn't return any result, or crashed during its operation.
     * <p/>
     * <p>You will receive this call immediately before onResume() when your
     * activity is re-starting.
     * <p/>
     *
     * @param requestCode The integer request code originally supplied to
     *                    startActivityForResult(), allowing you to identify who this
     *                    result came from.
     * @param resultCode  The integer result code returned by the child activity
     *                    through its setResult().
     * @param data        An Intent, which can return result data to the caller
     *                    (various data can be attached to Intent "extras").
     * @see #startActivityForResult
     * @see #createPendingResult
     * @see #setResult(int)
     */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RC_BARCODE_CAPTURE) {
        if (resultCode == CommonStatusCodes.SUCCESS) {
            if (data != null) {
                Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
                statusMessage.setText(R.string.barcode_success);
                barcodeValue.setText(barcode.displayValue);
                Log.d(TAG, "Barcode read: " + barcode.displayValue);
            } else {
                statusMessage.setText(R.string.barcode_failure);
                Log.d(TAG, "No barcode captured, intent data is null");
            }
        } else {
            statusMessage.setText(String.format(getString(R.string.barcode_error), CommonStatusCodes.getStatusCodeString(resultCode)));
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
Also used : Barcode(com.google.android.gms.vision.barcode.Barcode)

Example 2 with Barcode

use of com.google.android.gms.vision.barcode.Barcode in project PairingExample by AinaWireless.

the class MainActivity method onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PHOTO_REQUEST && resultCode == RESULT_OK) {
        mButton_scan.setEnabled(false);
        launchMediaScanIntent();
        try {
            Bitmap bitmap = decodeBitmapUri(this, imageUri);
            if (detector.isOperational() && bitmap != null) {
                Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                SparseArray<Barcode> QRcodes = detector.detect(frame);
                for (int index = 0; index < QRcodes.size(); index++) {
                    int tmp;
                    mac_cnt = 0;
                    scanHeader.setTypeface(null, Typeface.BOLD);
                    scanHeader.setText("QR-Code content:");
                    Barcode code = QRcodes.valueAt(index);
                    scanResults.setText(code.displayValue);
                    tmp = code.displayValue.indexOf("/");
                    String MAC = code.displayValue.substring(0, tmp);
                    MACs[mac_cnt] = MAC.substring(0, 2) + ":";
                    MACs[mac_cnt] += MAC.substring(2, 4) + ":";
                    MACs[mac_cnt] += MAC.substring(4, 6) + ":";
                    MACs[mac_cnt] += MAC.substring(6, 8) + ":";
                    MACs[mac_cnt] += MAC.substring(8, 10) + ":";
                    MACs[mac_cnt] += MAC.substring(10, 12);
                    mac_cnt++;
                    tmp = code.displayValue.indexOf("/") + 1;
                    MAC = code.displayValue.substring(tmp, code.displayValue.length());
                    MACs[mac_cnt] = MAC.substring(0, 2) + ":";
                    MACs[mac_cnt] += MAC.substring(2, 4) + ":";
                    MACs[mac_cnt] += MAC.substring(4, 6) + ":";
                    MACs[mac_cnt] += MAC.substring(6, 8) + ":";
                    MACs[mac_cnt] += MAC.substring(8, 10) + ":";
                    MACs[mac_cnt] += MAC.substring(10, 12);
                    if (!MACs[1].equals(""))
                        mText_classicMac.setText("BTC MAC address = " + MACs[1]);
                    if (!MACs[0].equals(""))
                        mText_bleMac.setText("BLE MAC address = " + MACs[0]);
                    tryBTC = true;
                    TextUpdateHandler.post(updateRunnable);
                    Runnable r = new ConnectThread(MACs[1]);
                    new Thread(r).start();
                }
                if (QRcodes.size() == 0) {
                    mButton_scan.setEnabled(true);
                    scanHeader.setText("Scan Failed: Did not found valid QR-Code");
                }
            } else {
                mButton_scan.setEnabled(true);
                scanHeader.setText("Could not set up the QR-scanner!");
            }
        } catch (Exception e) {
            Toast.makeText(this, "Failed to load QR-code file", Toast.LENGTH_SHORT).show();
        }
        File photo = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
        if (!photo.delete()) {
            Toast.makeText(this, "Failed to delete qr-code picture file", Toast.LENGTH_SHORT).show();
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) Frame(com.google.android.gms.vision.Frame) Barcode(com.google.android.gms.vision.barcode.Barcode) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) MainThread(android.support.annotation.MainThread) UiThread(android.support.annotation.UiThread)

Example 3 with Barcode

use of com.google.android.gms.vision.barcode.Barcode in project android-vision by googlesamples.

the class BarcodeCaptureActivity method onTap.

/**
     * onTap returns the tapped barcode result to the calling Activity.
     *
     * @param rawX - the raw position of the tap
     * @param rawY - the raw position of the tap.
     * @return true if the activity is ending.
     */
private boolean onTap(float rawX, float rawY) {
    // Find tap point in preview frame coordinates.
    int[] location = new int[2];
    mGraphicOverlay.getLocationOnScreen(location);
    float x = (rawX - location[0]) / mGraphicOverlay.getWidthScaleFactor();
    float y = (rawY - location[1]) / mGraphicOverlay.getHeightScaleFactor();
    // Find the barcode whose center is closest to the tapped point.
    Barcode best = null;
    float bestDistance = Float.MAX_VALUE;
    for (BarcodeGraphic graphic : mGraphicOverlay.getGraphics()) {
        Barcode barcode = graphic.getBarcode();
        if (barcode.getBoundingBox().contains((int) x, (int) y)) {
            // Exact hit, no need to keep looking.
            best = barcode;
            break;
        }
        float dx = x - barcode.getBoundingBox().centerX();
        float dy = y - barcode.getBoundingBox().centerY();
        // actually squared distance
        float distance = (dx * dx) + (dy * dy);
        if (distance < bestDistance) {
            best = barcode;
            bestDistance = distance;
        }
    }
    if (best != null) {
        Intent data = new Intent();
        data.putExtra(BarcodeObject, best);
        setResult(CommonStatusCodes.SUCCESS, data);
        finish();
        return true;
    }
    return false;
}
Also used : Barcode(com.google.android.gms.vision.barcode.Barcode) Intent(android.content.Intent)

Example 4 with Barcode

use of com.google.android.gms.vision.barcode.Barcode in project android-vision by googlesamples.

the class BarcodeGraphic method draw.

/**
     * Draws the barcode annotations for position, size, and raw value on the supplied canvas.
     */
@Override
public void draw(Canvas canvas) {
    Barcode barcode = mBarcode;
    if (barcode == null) {
        return;
    }
    // Draws the bounding box around the barcode.
    RectF rect = new RectF(barcode.getBoundingBox());
    rect.left = translateX(rect.left);
    rect.top = translateY(rect.top);
    rect.right = translateX(rect.right);
    rect.bottom = translateY(rect.bottom);
    canvas.drawRect(rect, mRectPaint);
    // Draws a label at the bottom of the barcode indicate the barcode value that was detected.
    canvas.drawText(barcode.rawValue, rect.left, rect.bottom, mTextPaint);
}
Also used : RectF(android.graphics.RectF) Barcode(com.google.android.gms.vision.barcode.Barcode)

Example 5 with Barcode

use of com.google.android.gms.vision.barcode.Barcode in project android-vision by googlesamples.

the class BarcodeGraphic method draw.

/**
     * Draws the barcode annotations for position, size, and raw value on the supplied canvas.
     */
@Override
public void draw(Canvas canvas) {
    Barcode barcode = mBarcode;
    if (barcode == null) {
        return;
    }
    // Draws the bounding box around the barcode.
    RectF rect = new RectF(barcode.getBoundingBox());
    rect.left = translateX(rect.left);
    rect.top = translateY(rect.top);
    rect.right = translateX(rect.right);
    rect.bottom = translateY(rect.bottom);
    canvas.drawRect(rect, mRectPaint);
    // Draws a label at the bottom of the barcode indicate the barcode value that was detected.
    canvas.drawText(barcode.rawValue, rect.left, rect.bottom, mTextPaint);
}
Also used : RectF(android.graphics.RectF) Barcode(com.google.android.gms.vision.barcode.Barcode)

Aggregations

Barcode (com.google.android.gms.vision.barcode.Barcode)5 RectF (android.graphics.RectF)2 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 MainThread (android.support.annotation.MainThread)1 UiThread (android.support.annotation.UiThread)1 Frame (com.google.android.gms.vision.Frame)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1