Skip to content

Commit

Permalink
Return tapped (or closest) barcode result to calling Activity.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchx committed Sep 30, 2016
1 parent 9278a0c commit e39a8e6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,41 +325,51 @@ private void startCameraSource() throws SecurityException {
}

/**
* onTap is called to capture the oldest barcode currently detected and
* return it to the caller.
* 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) {

//TODO: use the tap position to select the barcode.
BarcodeGraphic graphic = mGraphicOverlay.getFirstGraphic();
Barcode barcode = null;
if (graphic != null) {
barcode = graphic.getBarcode();
if (barcode != null) {
Intent data = new Intent();
data.putExtra(BarcodeObject, barcode);
setResult(CommonStatusCodes.SUCCESS, data);
finish();
// 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;
}
else {
Log.d(TAG, "barcode data is null");
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;
}
}
else {
Log.d(TAG,"no barcode detected");

if (best != null) {
Intent data = new Intent();
data.putExtra(BarcodeObject, best);
setResult(CommonStatusCodes.SUCCESS, data);
finish();
return true;
}
return barcode != null;
return false;
}

private class CaptureGestureListener extends GestureDetector.SimpleOnGestureListener {

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {

return onTap(e.getRawX(), e.getRawY()) || super.onSingleTapConfirmed(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import com.google.android.gms.vision.CameraSource;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

/**
* A view which renders a series of custom graphics to be overlayed on top of an associated preview
Expand Down Expand Up @@ -51,7 +53,6 @@ public class GraphicOverlay<T extends GraphicOverlay.Graphic> extends View {
private float mHeightScaleFactor = 1.0f;
private int mFacing = CameraSource.CAMERA_FACING_BACK;
private Set<T> mGraphics = new HashSet<>();
private T mFirstGraphic;

/**
* Base class for a custom graphics object to be rendered within the graphic overlay. Subclass
Expand Down Expand Up @@ -129,7 +130,6 @@ public GraphicOverlay(Context context, AttributeSet attrs) {
public void clear() {
synchronized (mLock) {
mGraphics.clear();
mFirstGraphic = null;
}
postInvalidate();
}
Expand All @@ -140,9 +140,6 @@ public void clear() {
public void add(T graphic) {
synchronized (mLock) {
mGraphics.add(graphic);
if (mFirstGraphic == null) {
mFirstGraphic = graphic;
}
}
postInvalidate();
}
Expand All @@ -153,24 +150,34 @@ public void add(T graphic) {
public void remove(T graphic) {
synchronized (mLock) {
mGraphics.remove(graphic);
if (mFirstGraphic != null && mFirstGraphic.equals(graphic)) {
mFirstGraphic = null;
}
}
postInvalidate();
}

/**
* Returns the first (oldest) graphic added. This is used
* to get the barcode that was detected first.
* @return graphic containing the barcode, or null if no barcodes are detected.
* Returns a copy (as a list) of the set of all active graphics.
* @return list of all active graphics.
*/
public T getFirstGraphic() {
public List<T> getGraphics() {
synchronized (mLock) {
return mFirstGraphic;
return new Vector(mGraphics);
}
}

/**
* Returns the horizontal scale factor.
*/
public float getWidthScaleFactor() {
return mWidthScaleFactor;
}

/**
* Returns the vertical scale factor.
*/
public float getHeightScaleFactor() {
return mHeightScaleFactor;
}

/**
* Sets the camera attributes for size and facing direction, which informs how to transform
* image coordinates later.
Expand Down

0 comments on commit e39a8e6

Please sign in to comment.