com.google.android.gms.vision.barcode.Barcode Java Examples

The following examples show how to use com.google.android.gms.vision.barcode.Barcode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PandroidScannerView.java    From pandroid with Apache License 2.0 6 votes vote down vote up
@Override
public void receiveDetections(final Detector.Detections<Barcode> var1) {
    if (isResume && var1.getDetectedItems().size() > 0) {
        pauseDetector();
        final Barcode barcode = var1.getDetectedItems().valueAt(0);
        handler.post(new Runnable() {
            @Override
            public void run() {
                if(!handleBarcode(barcode)){
                    resumeDetector();
                }
            }
        });

    }
}
 
Example #2
Source File: BarcodeFragment.java    From Barcode-Reader with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onScannedMultiple(List<Barcode> barcodes) {
    Log.e(TAG, "onScannedMultiple: " + barcodes.size());

    String codes = "";
    for (Barcode barcode : barcodes) {
        codes += barcode.displayValue + ", ";
    }

    final String finalCodes = codes;
    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getActivity(), "Barcodes: " + finalCodes, Toast.LENGTH_SHORT).show();
        }
    });
}
 
Example #3
Source File: MainActivity.java    From Barcode-Reader with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onScannedMultiple(List<Barcode> barcodes) {
    Log.e(TAG, "onScannedMultiple: " + barcodes.size());

    String codes = "";
    for (Barcode barcode : barcodes) {
        codes += barcode.displayValue + ", ";
    }

    final String finalCodes = codes;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), "Barcodes: " + finalCodes, Toast.LENGTH_SHORT).show();
        }
    });
}
 
Example #4
Source File: BarcodeGraphic.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: ScreenshotDecorator.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 6 votes vote down vote up
private void detectBarcode() {
    if (mDetectBarcodeTask != null && !mDetectBarcodeTask.isCancelled()) {
        mDetectBarcodeTask.cancel(true);
    }
    mDetectBarcodeTask = CompletableFuture.runAsync(() -> {
        try {
            final FirebaseVisionImage image = FirebaseVisionImage.fromFilePath(context, recentShotUri);
            mBarcodeDetector.detectInImage(image)
                    .addOnSuccessListener(Executors.mainThread(), res -> {
                        final ArrayList<Barcode> values = (ArrayList<Barcode>) res.stream()
                                .map(MyFirebaseHelper::getBarcode)
                                .collect(Collectors.toList());
                        if (!values.isEmpty()) {
                            showBarcodeAction(values);
                        }
                    });
        } catch (IOException e) {
            Log.e(TAG, "Failed to detect barcode.", e);
        }
    });
}
 
Example #6
Source File: BarcodeCentralFilter.java    From google-authenticator-android with Apache License 2.0 6 votes vote down vote up
/**
 * Checking if the barcode is inside the scanner square or not. As the found barcode's dimensions
 * is in camera's coordinates, we must convert it to canvas' coordinates.
 */
public static boolean barcodeIsInsideScannerSquare(Barcode barcode) {
  // When the savedSquareFrame or mCameraSize is not initialized yet, which means the layout is
  // not drawn, return false.
  if (mSavedSquareFrame == null || mCameraSize == null) {
    return false;
  }

  Rect barcodeFrameCamera = barcode.getBoundingBox();
  int left = barcodeFrameCamera.left;
  int top = barcodeFrameCamera.top;
  int right = barcodeFrameCamera.right;
  int bottom = barcodeFrameCamera.bottom;

  int cameraWidth = mCameraSize.getWidth();
  int cameraHeight = mCameraSize.getHeight();

  // Convert to canvas' coordinates
  Rect barcodeFrameCanvas = new Rect(
      left * mCanvasWidth / cameraWidth,
      top * mCanvasHeight / cameraHeight,
      right * mCanvasWidth / cameraWidth,
      bottom * mCanvasHeight / cameraHeight);

  return mSavedSquareFrame.contains(barcodeFrameCanvas);
}
 
Example #7
Source File: ViewBarcodeActivity.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final Intent intent = getIntent();
    if (intent == null || !ACTION.equals(intent.getAction()) || !intent.hasExtra(EXTRA_BARCODE)) {
        Log.e(TAG, "Received Intent is not valid.");
        finish();
        return;
    }

    List<Barcode> barcodeList = intent.getParcelableArrayListExtra(EXTRA_BARCODE);
    if (barcodeList.isEmpty()) {
        Log.e(TAG, "No Barcode");
        finish();
        return;
    }

    if (savedInstanceState == null) {
        ViewBarcodeDialog.newInstance(barcodeList)
                .show(getFragmentManager(), ViewBarcodeDialog.class.getSimpleName());
    }
}
 
Example #8
Source File: IntentUtils.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 6 votes vote down vote up
private static int convertPhoneTypeBarcodeToContract(int barcodePhoneType) {
    switch (barcodePhoneType) {
        case Barcode.Phone.WORK: {
            return ContactsContract.CommonDataKinds.Phone.TYPE_WORK;
        }
        case Barcode.Phone.HOME: {
            return ContactsContract.CommonDataKinds.Phone.TYPE_HOME;
        }
        case Barcode.Phone.MOBILE: {
            return ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
        }
        case Barcode.Phone.FAX: {
            return ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK;
        }
    }
    return -1;
}
 
Example #9
Source File: MainActivity.java    From Barcode-Reader with Apache License 2.0 6 votes vote down vote up
@Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != Activity.RESULT_OK) {
            Toast.makeText(this, "error in  scanning", Toast.LENGTH_SHORT).show();
            return;
        }

        if (requestCode == BARCODE_READER_ACTIVITY_REQUEST && data != null) {
            Barcode barcode = data.getParcelableExtra(BarcodeReaderActivity.KEY_CAPTURED_BARCODE);
            Toast.makeText(this, barcode.rawValue, Toast.LENGTH_SHORT).show();
            mTvResultHeader.setText("On Activity Result");
            mTvResult.setText(barcode.rawValue);
        }

    }
 
Example #10
Source File: BarcodeListAdapter.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 6 votes vote down vote up
void onBind(Barcode data) {
    mData = data;

    mText1.setText(data.rawValue);
    mText2.setText(mText2.getResources().getStringArray(R.array.barcode_types)[data.valueFormat]);

    switch (data.valueFormat) {
        case Barcode.URL: {
            onUrlItemBind();
            break;
        }
        case Barcode.PHONE: {
            onPhoneItemBind();
            break;
        }
        case Barcode.CONTACT_INFO: {
            onContactInfoItemBind();
            break;
        }
        default: {
            onNormalItemBind();
        }
    }
}
 
Example #11
Source File: BarcodeScannerView.java    From react-native-barcode-scanner-google with MIT License 6 votes vote down vote up
@Override
public Tracker<Barcode> create(Barcode barcode) {
    return new Tracker<Barcode>() {
        /**
         * Start tracking the detected item instance within the item overlay.
         */
        @Override
        public void onNewItem(int id, Barcode item) {
            // Act on new barcode found
            WritableMap event = Arguments.createMap();
            event.putString("data", item.displayValue);
            event.putString("type", BarcodeFormat.get(item.format));

            sendNativeEvent(BARCODE_FOUND_KEY, event);
        }
    };
}
 
Example #12
Source File: BarcodeCaptureFragment.java    From MVBarcodeReader with Apache License 2.0 6 votes vote down vote up
protected void onBarcodeDetected(final Barcode barcode){
    Log.d("BARCODE-SCANNER", "NEW BARCODE DETECTED");
    if (mMode == MVBarcodeScanner.ScanningMode.SINGLE_AUTO && mListener != null) {
        synchronized (mLock){
            if(!isListenerBusy) {
                isListenerBusy = true;
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (mCameraSource != null) mCameraSource.stop();
                        mListener.onBarcodeScanned(barcode);
                        isListenerBusy = false;
                    }
                });
            }
        }
    }
}
 
Example #13
Source File: BarcodeTrackerFactory.java    From android-vision with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #14
Source File: BarcodeFragment.java    From Barcode-Reader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onScanned(final Barcode barcode) {
    Log.e(TAG, "onScanned: " + barcode.displayValue);
    barcodeReader.playBeep();

    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getActivity(), "Barcode: " + barcode.displayValue, Toast.LENGTH_SHORT).show();
        }
    });
}
 
Example #15
Source File: BarcodeReaderFragment.java    From Barcode-Reader with Apache License 2.0 5 votes vote down vote up
@Override
public void onScanned(final Barcode barcode) {
    if (mListener != null && !isPaused) {
        if (getActivity() == null) {
            return;
        }
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mListener.onScanned(barcode);
            }
        });
    }
}
 
Example #16
Source File: BarcodeGraphicTracker.java    From flutter_mobile_vision with MIT License 5 votes vote down vote up
@Override
public void onNewItem(int id, Barcode barcode) {
    graphic.setId(id);
    if (barcodeUpdateListener != null) {
        barcodeUpdateListener.onBarcodeDetected(barcode);
    }
}
 
Example #17
Source File: BarcodeGraphic.java    From flutter_mobile_vision with MIT License 5 votes vote down vote up
/**
 * @return RectF that represents the graphic's bounding box.
 */
public RectF getBoundingBox() {
    Barcode barcode = this.barcode;
    if (barcode == null) {
        return null;
    }
    RectF rect = new RectF(barcode.getBoundingBox());
    rect = translateRect(rect);
    return rect;
}
 
Example #18
Source File: BarcodeTrackerFactory.java    From flutter_mobile_vision with MIT License 5 votes vote down vote up
@Override
public Tracker<Barcode> create(Barcode barcode) {
    BarcodeGraphic graphic = new BarcodeGraphic(graphicOverlay, showText);
    try {
        return new BarcodeGraphicTracker(graphicOverlay, graphic, barcodeUpdateListener);
    } catch (Exception ex) {
        Log.d("BarcodeTrackerFactory", ex.getMessage(), ex);
    }
    return null;
}
 
Example #19
Source File: BarcodeReaderFragment.java    From Barcode-Reader with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
        float distance = (dx * dx) + (dy * dy);  // actually squared distance
        if (distance < bestDistance) {
            best = barcode;
            bestDistance = distance;
        }
    }

    if (best != null) {
        Intent data = new Intent();
        data.putExtra(BarcodeObject, best);

        // TODO - pass the scanned value
        getActivity().setResult(CommonStatusCodes.SUCCESS, data);
        return true;
    }
    return false;
}
 
Example #20
Source File: CamActivity.java    From RestaurantApp with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRetrievedMultiple(final Barcode barcode, final List<BarcodeGraphic> barcodeGraphics) {
    /*
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(CamActivity.this, barcode.displayValue, Toast.LENGTH_SHORT).show();
        }
    });
    */
}
 
Example #21
Source File: BarcodeCaptureActivity.java    From samples-android with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
        float distance = (dx * dx) + (dy * dy);  // actually squared distance
        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;
}
 
Example #22
Source File: IssueDebugActivity.java    From MVBarcodeReader with Apache License 2.0 5 votes vote down vote up
@Override
public void onBarcodesScanned(List<Barcode> barcodes) {
    for(Barcode barcode:barcodes){
        logBarcode(barcode);
    }
    finish();
}
 
Example #23
Source File: CameraScanActivity.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public CameraScanActivity(int type) {
    super();
    CameraController.getInstance().initCamera(() -> {
        if (cameraView != null) {
            cameraView.initCamera();
        }
    });
    currentType = type;
    if (currentType == TYPE_QR) {
        qrReader = new QRCodeReader();
        visionQrReader = new BarcodeDetector.Builder(ApplicationLoader.applicationContext).setBarcodeFormats(Barcode.QR_CODE).build();
    }
}
 
Example #24
Source File: MainActivity.java    From Beginner-Level-Android-Studio-Apps with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.imageView);
    btnScan = (Button) findViewById(R.id.btnScan);
    txtResult = (TextView) findViewById(R.id.txtResult);

    final Bitmap myBitmap = BitmapFactory.decodeResource(getApplicationContext().
            getResources(), R.drawable.qrcode);
    imageView.setImageBitmap(myBitmap);

    btnScan.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            BarcodeDetector detector = new BarcodeDetector.Builder(getApplicationContext())
                    .setBarcodeFormats(Barcode.QR_CODE)
                    .build();

            Frame frame = new Frame.Builder()
                    .setBitmap(myBitmap)
                    .build();
            SparseArray<Barcode> barsCode = detector.detect(frame);

            Barcode result = barsCode.valueAt(0);
            txtResult.setText(result.rawValue);
        }
    });
}
 
Example #25
Source File: CamActivity.java    From RestaurantApp with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRetrieved(final Barcode barcode) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //sharedPref.setVariable("deskID",barcode.displayValue);
            //startActivity(new Intent(getApplicationContext(), UserActivity.class));
            //overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        }
    });

}
 
Example #26
Source File: IntentUtils.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 5 votes vote down vote up
private static int convertEmailTypeBarcodeToContract(int barcodeEmailType) {
    switch (barcodeEmailType) {
        case Barcode.Email.WORK: {
            return ContactsContract.CommonDataKinds.Email.TYPE_WORK;
        }
        case Barcode.Email.HOME: {
            return ContactsContract.CommonDataKinds.Email.TYPE_HOME;
        }
    }
    return -1;
}
 
Example #27
Source File: BarcodeCaptureActivity.java    From flutter_barcode_scanner with MIT License 5 votes vote down vote up
@Override
public void onClick(View v) {
    int i = v.getId();
    if (i == R.id.imgViewBarcodeCaptureUseFlash &&
            getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
        try {
            if (flashStatus == USE_FLASH.OFF.ordinal()) {
                flashStatus = USE_FLASH.ON.ordinal();
                imgViewBarcodeCaptureUseFlash.setImageResource(R.drawable.ic_barcode_flash_on);
                turnOnOffFlashLight(true);
            } else {
                flashStatus = USE_FLASH.OFF.ordinal();
                imgViewBarcodeCaptureUseFlash.setImageResource(R.drawable.ic_barcode_flash_off);
                turnOnOffFlashLight(false);
            }
        } catch (Exception e) {
            Toast.makeText(this, "Unable to turn on flash", Toast.LENGTH_SHORT).show();
            Log.e("BarcodeCaptureActivity", "FlashOnFailure: " + e.getLocalizedMessage());
        }
    } else if (i == R.id.btnBarcodeCaptureCancel) {
        Barcode barcode = new Barcode();
        barcode.rawValue = "-1";
        barcode.displayValue = "-1";
        FlutterBarcodeScannerPlugin.onBarcodeScanReceiver(barcode);
        finish();
    }
}
 
Example #28
Source File: QrDialogFragment.java    From octoandroid with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
    final SparseArray<Barcode> barcodes = detections.getDetectedItems();

    if (barcodes.size() != 0) {
        mApiKey = barcodes.valueAt(0).displayValue;
        getActivity().runOnUiThread(this);
    }
}
 
Example #29
Source File: ViewBarcodeActivity.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 5 votes vote down vote up
public static ViewBarcodeDialog newInstance(@NonNull List<Barcode> data) {
    final Bundle args = new Bundle();
    args.putParcelableArrayList(EXTRA_BARCODE, (ArrayList<Barcode>) data);
    final ViewBarcodeDialog dialog = new ViewBarcodeDialog();
    dialog.setArguments(args);
    return dialog;
}
 
Example #30
Source File: MyFirebaseHelper.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 5 votes vote down vote up
public static Barcode getBarcode(FirebaseVisionBarcode firebaseBarcode) {
    try {
        return (Barcode) sField_FirebaseVisionBarcode_barcode.get(firebaseBarcode);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}