Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autofocus as parameter #64

Merged
merged 8 commits into from
Aug 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ public void stopCamera() {
mCameraManager.stopPreview();
}

public void setAutofocusInterval(long autofocusIntervalInMs) {
if (mCameraManager != null) {
mCameraManager.setAutofocusInterval(autofocusIntervalInMs);
}
}

public void setTorchEnabled(boolean enabled) {
if (mCameraManager != null) {
mCameraManager.setTorchEnabled(enabled);
}
}

@Override public void onDetachedFromWindow() {
super.onDetachedFromWindow();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

package com.google.zxing.client.android.camera;

import android.content.Context;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.util.Log;
Expand All @@ -44,8 +43,9 @@ final class AutoFocusManager implements Camera.AutoFocusCallback {

private static final String TAG = AutoFocusManager.class.getSimpleName();

private static final long AUTO_FOCUS_INTERVAL_MS = 5000L;
protected static final long DEFAULT_AUTO_FOCUS_INTERVAL_MS = 5000L;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need this protected, to access this default value from other classes in the same package.
For example: https://github.com/dlazaro66/QRCodeReaderView/pull/64/files#diff-63e2aacd2af702b6dfe82ecf928463efR52

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

private static final Collection<String> FOCUS_MODES_CALLING_AF;
private long autofocusIntervalMs = DEFAULT_AUTO_FOCUS_INTERVAL_MS;

static {
FOCUS_MODES_CALLING_AF = new ArrayList<>(2);
Expand All @@ -59,7 +59,7 @@ final class AutoFocusManager implements Camera.AutoFocusCallback {
private final Camera camera;
private AsyncTask<?, ?, ?> outstandingTask;

AutoFocusManager(Context context, Camera camera) {
AutoFocusManager(Camera camera) {
this.camera = camera;
String currentFocusMode = camera.getParameters().getFocusMode();
useAutoFocus = FOCUS_MODES_CALLING_AF.contains(currentFocusMode);
Expand All @@ -72,6 +72,13 @@ final class AutoFocusManager implements Camera.AutoFocusCallback {
autoFocusAgainLater();
}

public void setAutofocusInterval(long autofocusIntervalMs) {
if (autofocusIntervalMs <= 0) {
throw new IllegalArgumentException("AutoFocusInterval must be greater than 0.");
}
this.autofocusIntervalMs = autofocusIntervalMs;
}

private synchronized void autoFocusAgainLater() {
if (!stopped && outstandingTask == null) {
AutoFocusTask newTask = new AutoFocusTask();
Expand Down Expand Up @@ -127,7 +134,7 @@ synchronized void stop() {
private final class AutoFocusTask extends AsyncTask<Object, Object, Object> {
@Override protected Object doInBackground(Object... voids) {
try {
Thread.sleep(AUTO_FOCUS_INTERVAL_MS);
Thread.sleep(autofocusIntervalMs);
} catch (InterruptedException e) {
// continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ void initFromCameraParameters(OpenCamera camera, int width, int height) {
}
Log.i(TAG, "Clockwise rotation from display to camera: " + cwNeededRotation);

Point viewDimensions = new Point(width,height);
resolution = viewDimensions;
resolution = new Point(width, height);
Log.i(TAG, "Screen resolution in current orientation: " + resolution);
cameraResolution = findBestPreviewSizeValue(parameters, resolution);
Log.i(TAG, "Camera resolution: " + cameraResolution);
Expand Down Expand Up @@ -288,24 +287,24 @@ boolean getTorchState(Camera camera) {
return false;
}

void setTorch(Camera camera, boolean newSetting) {
void setTorchEnabled(Camera camera, boolean enabled) {
Camera.Parameters parameters = camera.getParameters();
doSetTorch(parameters, newSetting, false);
setTorchEnabled(parameters, enabled, false);
camera.setParameters(parameters);
}

private void doSetTorch(Camera.Parameters parameters, boolean newSetting, boolean safeMode) {
setTorch(parameters, newSetting);
void setTorchEnabled(Camera.Parameters parameters, boolean enabled, boolean safeMode) {
setTorchEnabled(parameters, enabled);

if (!safeMode) {
setBestExposure(parameters, newSetting);
setBestExposure(parameters, enabled);
}
}

public static void setTorch(Camera.Parameters parameters, boolean on) {
public static void setTorchEnabled(Camera.Parameters parameters, boolean enabled) {
List<String> supportedFlashModes = parameters.getSupportedFlashModes();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setTorchEnabled(boolean enabled)

String flashMode;
if (on) {
if (enabled) {
flashMode =
findSettableValue("flash mode", supportedFlashModes, Camera.Parameters.FLASH_MODE_TORCH,
Camera.Parameters.FLASH_MODE_ON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final class CameraManager {
// PreviewCallback references are also removed from original ZXING authors work, since We're using our own interface
// FramingRects references are also removed from original ZXING authors work, since We're using all view size while detecting QR-Codes
private int requestedCameraId = OpenCameraInterface.NO_REQUESTED_CAMERA;
private long autofocusIntervalInMs = AutoFocusManager.DEFAULT_AUTO_FOCUS_INTERVAL_MS;

public CameraManager(Context context) {
this.context = context;
Expand All @@ -71,6 +72,13 @@ public void setDisplayOrientation(int degrees) {
}
}

public void setAutofocusInterval(long autofocusIntervalInMs) {
if (autoFocusManager != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot the:

this. autofocusIntervalInMs = autofocusIntervalInMs;

this.autofocusIntervalInMs = autofocusIntervalInMs;
autoFocusManager.setAutofocusInterval(autofocusIntervalInMs);
}
}

public Point getPreviewSize() {
return configManager.getCameraResolution();
}
Expand Down Expand Up @@ -137,20 +145,20 @@ public synchronized void setManualCameraId(int cameraId) {
}

/**
* @param newSetting if {@code true}, light should be turned on if currently off. And vice versa.
* @param enabled if {@code true}, light should be turned on if currently off. And vice versa.
*/
public synchronized void setTorch(boolean newSetting) {
public synchronized void setTorchEnabled(boolean enabled) {
OpenCamera theCamera = openCamera;
if (theCamera != null) {
if (newSetting != configManager.getTorchState(theCamera.getCamera())) {
if (enabled != configManager.getTorchState(theCamera.getCamera())) {
boolean wasAutoFocusManager = autoFocusManager != null;
if (wasAutoFocusManager) {
autoFocusManager.stop();
autoFocusManager = null;
}
configManager.setTorch(theCamera.getCamera(), newSetting);
configManager.setTorchEnabled(theCamera.getCamera(), enabled);
if (wasAutoFocusManager) {
autoFocusManager = new AutoFocusManager(context, theCamera.getCamera());
autoFocusManager = new AutoFocusManager(theCamera.getCamera());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why setting the torch on/off requires a new AutoFocusManager?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how original Zxing project is doing it. It seems like you need to create a new AutoFocusManager every time you change the configuration.

autoFocusManager.start();
}
}
Expand Down Expand Up @@ -183,7 +191,8 @@ public synchronized void startPreview() {
if (theCamera != null && !previewing) {
theCamera.getCamera().startPreview();
previewing = true;
autoFocusManager = new AutoFocusManager(context, theCamera.getCamera());
autoFocusManager = new AutoFocusManager(theCamera.getCamera());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why start previewing requires a new AutoFocusManager?

Copy link
Owner Author

@dlazaro66 dlazaro66 Aug 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how original Zxing project is doing it. It seems like you need to create a new AutoFocusManager every time you change the configuration.

autoFocusManager.setAutofocusInterval(autofocusIntervalInMs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class DecoderActivity extends Activity implements OnQRCodeReadListener {

mydecoderview = (QRCodeReaderView) findViewById(R.id.qrdecoderview);
mydecoderview.setOnQRCodeReadListener(this);
mydecoderview.setAutofocusInterval(1000L);

myTextView = (TextView) findViewById(R.id.exampleTextView);

Expand All @@ -36,7 +37,6 @@ public class DecoderActivity extends Activity implements OnQRCodeReadListener {
mAnimation.setRepeatMode(Animation.REVERSE);
mAnimation.setInterpolator(new LinearInterpolator());
line_image.setAnimation(mAnimation);

}

@Override protected void onResume() {
Expand Down