Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Added a flip-camera button for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Jul 26, 2014
1 parent 98f158a commit 1a2b8c6
Show file tree
Hide file tree
Showing 36 changed files with 69 additions and 42 deletions.
2 changes: 1 addition & 1 deletion plugin.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?><plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.phonegap.plugins.barcodescanner"
version="1.2.1">
version="1.2.2">

<name>BarcodeScanner</name>
<description>You can use the BarcodeScanner plugin to scan different types of barcodes (using the device's camera) and get the metadata encoded in them for processing within your application.</description>
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/android/LibraryProject/res/layout-ldpi/capture.xml 100755 → 100644
Expand Up @@ -216,4 +216,11 @@
android:text="@string/msg_default_status"
android:textColor="@color/status_text"/>

<Button android:id="@+id/flip_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:drawableLeft="@drawable/flip_camera"
android:visibility="invisible"/>

</FrameLayout>
7 changes: 7 additions & 0 deletions src/android/LibraryProject/res/layout/capture.xml 100755 → 100644
Expand Up @@ -215,4 +215,11 @@
android:text="@string/msg_default_status"
android:textColor="@color/status_text"/>

<Button android:id="@+id/flip_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:drawableLeft="@drawable/flip_camera"
android:visibility="invisible"/>

</FrameLayout>
24 changes: 22 additions & 2 deletions src/android/LibraryProject/src/com/google/zxing/client/android/CaptureActivity.java 100755 → 100644
Expand Up @@ -16,6 +16,8 @@

package com.google.zxing.client.android;

import android.hardware.Camera;
import android.widget.Button;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.google.zxing.ResultMetadataType;
Expand All @@ -28,6 +30,7 @@
import com.google.zxing.client.android.result.ResultHandler;
import com.google.zxing.client.android.result.ResultHandlerFactory;
import com.google.zxing.client.android.result.supplement.SupplementalInfoRetriever;
import com.google.zxing.client.android.share.BookmarkPickerActivity;
import com.google.zxing.client.android.share.ShareActivity;

import android.app.Activity;
Expand Down Expand Up @@ -110,6 +113,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
private Result savedResultToShow;
private ViewfinderView viewfinderView;
private TextView statusView;
private Button flipButton;
private View resultView;
private Result lastResult;
private boolean hasSurface;
Expand Down Expand Up @@ -172,6 +176,7 @@ protected void onResume() {

resultView = findViewById(fakeR.getId("id", "result_view"));
statusView = (TextView) findViewById(fakeR.getId("id", "status_view"));
flipButton = (Button) findViewById(fakeR.getId("id", "flip_button"));

handler = null;
lastResult = null;
Expand Down Expand Up @@ -701,8 +706,11 @@ private void initCamera(SurfaceHolder surfaceHolder) {
Log.w(TAG, "initCamera() while already open -- late SurfaceView callback?");
return;
}

boolean preferFrontCamera = getIntent().getBooleanExtra(Intents.Scan.PREFER_FRONTCAMERA, false);

try {
cameraManager.openDriver(surfaceHolder);
cameraManager.openDriver(surfaceHolder, preferFrontCamera);
// Creating the handler starts the preview, which can also throw a RuntimeException.
if (handler == null) {
handler = new CaptureActivityHandler(this, decodeFormats, characterSet, cameraManager);
Expand Down Expand Up @@ -738,9 +746,21 @@ public void restartPreviewAfterDelay(long delayMS) {
private void resetStatusView() {
resultView.setVisibility(View.GONE);
statusView.setText(fakeR.getId("string", "msg_default_status"));
statusView.setVisibility(View.VISIBLE);
viewfinderView.setVisibility(View.VISIBLE);
lastResult = null;

// in case the device has multiple camera's, offer a flip button
if (Camera.getNumberOfCameras() > 1) { //
flipButton.setVisibility(View.VISIBLE);
flipButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
getIntent().putExtra(Intents.Scan.PREFER_FRONTCAMERA,
!getIntent().hasExtra(Intents.Scan.PREFER_FRONTCAMERA) ||
!getIntent().getBooleanExtra(Intents.Scan.PREFER_FRONTCAMERA, false));
recreate();
}});
}
}

public void drawViewfinder() {
Expand Down
5 changes: 5 additions & 0 deletions src/android/LibraryProject/src/com/google/zxing/client/android/Intents.java 100755 → 100644
Expand Up @@ -153,6 +153,11 @@ public static final class Scan {
*/
public static final String SAVE_HISTORY = "SAVE_HISTORY";

/**
* Set to true if we should scan with the frontcamera (if there is one)
*/
public static final String PREFER_FRONTCAMERA = "PREFER_FRONTCAMERA";

private Scan() {
}
}
Expand Down
Expand Up @@ -44,7 +44,6 @@
import com.google.zxing.client.android.Intents;
import com.google.zxing.client.android.HttpHelper;
import com.google.zxing.client.android.LocaleManager;
import com.google.zxing.client.android.R;
import com.google.zxing.client.android.common.executor.AsyncTaskExecInterface;
import com.google.zxing.client.android.common.executor.AsyncTaskExecManager;

Expand Down
Expand Up @@ -25,8 +25,6 @@

import java.util.List;

import com.google.zxing.client.android.R;

/**
* Manufactures list items which represent SBC results.
*
Expand Down
Expand Up @@ -16,8 +16,6 @@

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

import com.google.zxing.client.android.R;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Spannable;
Expand Down
5 changes: 3 additions & 2 deletions src/android/LibraryProject/src/com/google/zxing/client/android/camera/CameraManager.java 100755 → 100644
Expand Up @@ -70,12 +70,13 @@ public CameraManager(Context context) {
* Opens the camera driver and initializes the hardware parameters.
*
* @param holder The surface object which the camera will draw preview frames into.
* @param preferFrontCamera Indicates whether or not we prefer the front cam over the one on the back of the device.
* @throws IOException Indicates the camera driver failed to open.
*/
public synchronized void openDriver(SurfaceHolder holder) throws IOException {
public synchronized void openDriver(SurfaceHolder holder, boolean preferFrontCamera) throws IOException {
Camera theCamera = camera;
if (theCamera == null) {
theCamera = new OpenCameraManager().build().open();
theCamera = new OpenCameraManager().build().open(preferFrontCamera);
if (theCamera == null) {
throw new IOException();
}
Expand Down
Expand Up @@ -27,7 +27,8 @@ final class DefaultOpenCameraInterface implements OpenCameraInterface {
* Calls {@link Camera#open()}.
*/
@Override
public Camera open() {
public Camera open(boolean preferFrontCamera) {
// Note: preferFrontCamera ignored on purpose (low value, lot of test effort)
return Camera.open();
}

Expand Down
Expand Up @@ -33,8 +33,7 @@ public final class GingerbreadOpenCameraInterface implements OpenCameraInterface
* Opens a rear-facing camera with {@link Camera#open(int)}, if one exists, or opens camera 0.
*/
@Override
public Camera open() {

public Camera open(boolean preferFrontCamera) {
int numCameras = Camera.getNumberOfCameras();
if (numCameras == 0) {
Log.w(TAG, "No cameras!");
Expand All @@ -45,18 +44,24 @@ public Camera open() {
while (index < numCameras) {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(index, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
break;
if (preferFrontCamera) {
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
break;
}
} else {
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
break;
}
}
index++;
}

Camera camera;
if (index < numCameras) {
Log.i(TAG, "Opening camera #" + index);
camera = Camera.open(index);
} else {
Log.i(TAG, "No camera facing back; returning camera #0");
Log.i(TAG, "No camera found according to preference; returning camera #0");
camera = Camera.open(0);
}

Expand Down
Expand Up @@ -24,6 +24,6 @@
*/
public interface OpenCameraInterface {

Camera open();
Camera open(boolean preferFrontCamera);

}
Expand Up @@ -23,7 +23,6 @@
import com.google.zxing.client.android.Contents;
import com.google.zxing.client.android.FinishListener;
import com.google.zxing.client.android.Intents;
import com.google.zxing.client.android.R;

import android.app.Activity;
import android.app.AlertDialog;
Expand Down
Expand Up @@ -24,7 +24,6 @@
import com.google.zxing.WriterException;
import com.google.zxing.client.android.Contents;
import com.google.zxing.client.android.Intents;
import com.google.zxing.client.android.R;
import com.google.zxing.client.result.AddressBookParsedResult;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ResultParser;
Expand Down
Expand Up @@ -35,7 +35,6 @@
import com.google.zxing.FakeR;
import com.google.zxing.client.android.CaptureActivity;
import com.google.zxing.client.android.Intents;
import com.google.zxing.client.android.R;

import java.util.List;

Expand Down
Expand Up @@ -26,7 +26,6 @@
import android.widget.TextView;
import com.google.zxing.FakeR;
import com.google.zxing.Result;
import com.google.zxing.client.android.R;

import java.util.ArrayList;

Expand Down
Expand Up @@ -16,7 +16,7 @@

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

import com.google.zxing.client.android.R;

import com.google.zxing.client.result.AddressBookParsedResult;
import com.google.zxing.client.result.ParsedResult;

Expand Down
Expand Up @@ -18,7 +18,7 @@

import android.content.ActivityNotFoundException;
import android.util.Log;
import com.google.zxing.client.android.R;

import com.google.zxing.client.result.CalendarParsedResult;
import com.google.zxing.client.result.ParsedResult;

Expand Down
Expand Up @@ -16,7 +16,6 @@

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

import com.google.zxing.client.android.R;
import com.google.zxing.client.result.EmailAddressParsedResult;
import com.google.zxing.client.result.ParsedResult;

Expand Down
Expand Up @@ -16,7 +16,6 @@

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

import com.google.zxing.client.android.R;
import com.google.zxing.client.result.GeoParsedResult;
import com.google.zxing.client.result.ParsedResult;

Expand Down
Expand Up @@ -17,7 +17,7 @@
package com.google.zxing.client.android.result;

import com.google.zxing.Result;
import com.google.zxing.client.android.R;

import com.google.zxing.client.result.ISBNParsedResult;
import com.google.zxing.client.result.ParsedResult;

Expand Down
Expand Up @@ -17,7 +17,7 @@
package com.google.zxing.client.android.result;

import com.google.zxing.Result;
import com.google.zxing.client.android.R;

import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ProductParsedResult;

Expand Down
Expand Up @@ -21,7 +21,7 @@
import com.google.zxing.client.android.Intents;
import com.google.zxing.client.android.LocaleManager;
import com.google.zxing.client.android.PreferencesActivity;
import com.google.zxing.client.android.R;

import com.google.zxing.client.android.book.SearchBookContentsActivity;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ParsedResultType;
Expand Down
Expand Up @@ -16,7 +16,6 @@

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

import com.google.zxing.client.android.R;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.SMSParsedResult;

Expand Down
Expand Up @@ -16,7 +16,6 @@

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

import com.google.zxing.client.android.R;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.TelParsedResult;

Expand Down
Expand Up @@ -17,7 +17,7 @@
package com.google.zxing.client.android.result;

import com.google.zxing.Result;
import com.google.zxing.client.android.R;

import com.google.zxing.client.result.ParsedResult;

import android.app.Activity;
Expand Down
Expand Up @@ -17,7 +17,6 @@
package com.google.zxing.client.android.result;

import com.google.zxing.client.android.LocaleManager;
import com.google.zxing.client.android.R;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.URIParsedResult;

Expand Down
Expand Up @@ -21,7 +21,7 @@
import android.widget.Toast;

import com.google.zxing.client.android.CaptureActivity;
import com.google.zxing.client.android.R;

import com.google.zxing.client.android.common.executor.AsyncTaskExecInterface;
import com.google.zxing.client.android.common.executor.AsyncTaskExecManager;
import com.google.zxing.client.android.wifi.WifiConfigManager;
Expand Down
Expand Up @@ -25,7 +25,6 @@
import com.google.zxing.FakeR;
import com.google.zxing.client.android.HttpHelper;
import com.google.zxing.client.android.LocaleManager;
import com.google.zxing.client.android.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down
Expand Up @@ -21,7 +21,6 @@
import android.widget.TextView;
import com.google.zxing.FakeR;
import com.google.zxing.client.android.HttpHelper;
import com.google.zxing.client.android.R;
import com.google.zxing.client.android.history.HistoryManager;
import com.google.zxing.client.android.LocaleManager;

Expand Down
Expand Up @@ -21,7 +21,6 @@
import com.google.zxing.FakeR;
import com.google.zxing.client.android.HttpHelper;
import com.google.zxing.client.android.history.HistoryManager;
import com.google.zxing.client.android.R;
import com.google.zxing.client.result.URIParsedResult;

import java.io.IOException;
Expand Down
Expand Up @@ -16,8 +16,6 @@

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

import com.google.zxing.client.android.R;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
Expand Down
Expand Up @@ -20,7 +20,6 @@
import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.android.Contents;
import com.google.zxing.client.android.Intents;
import com.google.zxing.client.android.R;

import android.app.Activity;
import android.content.ContentResolver;
Expand Down

0 comments on commit 1a2b8c6

Please sign in to comment.