Skip to content

Commit

Permalink
Minor API tweaks, added properties controlling barcode tracking and f…
Browse files Browse the repository at this point in the history
…ocus lock
  • Loading branch information
tschaumburg committed Dec 11, 2015
1 parent ae2b33f commit eaa21e9
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 17 deletions.
18 changes: 18 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MainActivity extends AppCompatActivity
{
private static final String TAG = "FastBarcodeScannerDemo";
private SurfaceView mSurfaceView;
private TextureView mTextureView;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -56,7 +57,8 @@ public void onClick(View view) {
}
});

mSurfaceView = (SurfaceView)findViewById(R.id.surfaceView);
//mSurfaceView = (SurfaceView)findViewById(R.id.preview);
mTextureView = (TextureView)findViewById(R.id.preview);
}

@Override
Expand Down Expand Up @@ -86,15 +88,16 @@ private void startScan() {
requestCameraPermission();

if (mScanner == null) {
mScanner = new FastBarcodeScanner(this, (TextureView)null);
//mScanner = new FastBarcodeScanner(this, mSurfaceView);
//mScanner = new FastBarcodeScanner(this, (TextureView)null);
mScanner = new FastBarcodeScanner(this, mTextureView);
//mScanner.setScanningStateListener(this);
}

Button startButton = (Button)findViewById(R.id.start);
Button stopButton = (Button)findViewById(R.id.button3);

startButton.setEnabled(false);
mScanner.setLockFocus(false);
mScanner.StartScan(this, null);
stopButton.setEnabled(true);
}
Expand Down
13 changes: 10 additions & 3 deletions fast-barcode-scanner-demo/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:id="@+id/textView2" />
<SurfaceView
<!--<SurfaceView
android:layout_width="1dp"
android:layout_height="1dp"
android:id="@+id/surfaceView"
android:id="@+id/preview"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp" />-->
<TextureView
android:layout_width="wrap_content"
android:layout_height="140pt"
android:id="@+id/preview"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp" />
Expand All @@ -40,7 +47,7 @@
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="10pt"
android:layout_below="@+id/surfaceView"
android:layout_below="@+id/preview"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/textView"
android:indeterminate="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ private Activity getActivity() {
return mActivity;
}

private IStillSequenceCamera mImageSource;
private TrackingBarcodeScanner mBarcodeFinder;
private final IStillSequenceCamera mImageSource;
private final TrackingBarcodeScanner mBarcodeFinder;

/**
* Creates a headless FastBarcodeScanner (i.e. one without any UI)
Expand All @@ -88,6 +88,10 @@ private Activity getActivity() {
* As an alternative, consider using the #FastBarcodeScanner constructor
* which will create a FastBarcodeScanner working on older versions of
* Android too - albeit much less efficiently.
*
* The following properties control the behaviour of the barcode scanning
* (see #TrackingBarcodeScanner for details): UseTracking, RelativeTrackingMargin,
* NoHitsBeforeTrackingLoss.
* @param activity Non null
*/
@TargetApi(21)
Expand Down Expand Up @@ -393,5 +397,12 @@ public void setUseTracking(boolean useTracking) {
mBarcodeFinder.setUseTracking(useTracking);
}

public boolean isLockFocus() {
return mImageSource.isLockFocus();
}

public void setLockFocus(boolean lockFocus) {
mImageSource.setLockFocus(lockFocus);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ public interface OnImageAvailableListener
void onImageAvailable(int format, byte[] data, int width, int height);
void onError(Exception error);
}

boolean isLockFocus();
void setLockFocus(boolean lockFocus);
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PictureCallback;
import android.media.Image;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import java.io.IOException;

Expand All @@ -30,6 +28,7 @@ public class StillSequenceCamera implements IStillSequenceCamera {
private final static int CLOSED = 0;
private final static int INITIALIZED = 1;
private final static int CAPTURING = 2;
private boolean mLockFocus = true;
private int mState = CLOSED;

public StillSequenceCamera(Activity activity, SurfaceView preview)
Expand Down Expand Up @@ -97,6 +96,7 @@ public void setup()
*
* @throws IllegalStateException if the StillSequenceCamera is in any but the INITIALIZED state
*/
@Override
public void start(OnImageAvailableListener listener, Handler callbackHandler)
throws IllegalStateException
{
Expand Down Expand Up @@ -267,4 +267,14 @@ public void run() {
}
);
}

@Override
public boolean isLockFocus() {
return mLockFocus;
}

@Override
public void setLockFocus(boolean lockFocus) {
this.mLockFocus = lockFocus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,13 @@ public static interface FocusListener
* looper}.
* @param listener
*/
public void start(CameraCaptureSession cameraCaptureSession, Handler callbackHandler, FocusListener listener)
public void start(CameraCaptureSession cameraCaptureSession, boolean lockFocus, Handler callbackHandler, FocusListener listener)
{
mCameraCaptureSession = cameraCaptureSession;
// When the session is ready, we start displaying the preview.
try {
Log.i(TAG, "StartFocusing");
mStateMachine = new FocusingStateMachine(cameraCaptureSession, callbackHandler, listener);
mStateMachine = new FocusingStateMachine(cameraCaptureSession, lockFocus, callbackHandler, listener);
mStateMachine.start(mPreviewSurface);
} catch (Exception e) {
if (cameraCaptureSession != null) {
Expand Down Expand Up @@ -325,6 +325,7 @@ private class FocusingStateMachine extends CameraCaptureSession.CaptureCallback
private final FocusListener mListener;
private final Handler mCallbackHandler;
private final CameraCaptureSession mCaptureSession;
private final boolean mLockFocus;

private HandlerThread mFocusingStateMachineThread;
private Handler mFocusingStateMachineHandler;
Expand All @@ -340,8 +341,9 @@ private class FocusingStateMachine extends CameraCaptureSession.CaptureCallback
private static final int STATE_PICTURE_TAKEN = 4;
private int mState = STATE_IDLE;

FocusingStateMachine(CameraCaptureSession cameraCaptureSession, Handler callbackHandler, FocusListener listener) {
FocusingStateMachine(CameraCaptureSession cameraCaptureSession, boolean lockFocus, Handler callbackHandler, FocusListener listener) {
mState = STATE_IDLE;
mLockFocus = lockFocus;
mCaptureSession = cameraCaptureSession;
mListener = listener;
if (callbackHandler != null) {
Expand Down Expand Up @@ -558,7 +560,8 @@ private void onFocusLocked()
{
Log.i(TAG, "focus lock");
try {
mCaptureSession.stopRepeating();
if (mLockFocus)
mCaptureSession.stopRepeating();
if (mListener != null) {
mCallbackHandler.post(
new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.Image;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
//import android.support.annotation.NonNull;
Expand Down Expand Up @@ -54,6 +52,7 @@ public class StillSequenceCamera2 implements IStillSequenceCamera {
private final static int FAILED = 5;
private final static int ERROR = 6;
private int mState = CLOSED;
private boolean mLockFocus = true;

/**
* Creates a headless #StillSequenceCamera2
Expand Down Expand Up @@ -199,14 +198,16 @@ public void onConfigured(CameraCaptureSession cameraCaptureSession) {
mState = FOCUSING;
mFocusManager.start(
mCaptureSession,
mLockFocus,
mFocusHandler,
new FocusManager.FocusListener() {
@Override
public void focusLocked() {
//startCapturePhase();
mState = CAPTURING;
mImageCapture.start(mCaptureSession, _callbackHandler, listener);
mFocusManager.stop();
if (mLockFocus)
mFocusManager.stop();
}

@Override
Expand Down Expand Up @@ -348,4 +349,14 @@ public void close() {

mState = CLOSED;
}

@Override
public boolean isLockFocus() {
return mLockFocus;
}

@Override
public void setLockFocus(boolean lockFocus) {
this.mLockFocus = lockFocus;
}
}

0 comments on commit eaa21e9

Please sign in to comment.