Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

Allow possibility to open the front camera #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -56,6 +56,7 @@ public class SampleActivity extends Activity {
private CheckBox mUseCardIOLogoToggle;
private CheckBox mShowPayPalActionBarIconToggle;
private CheckBox mKeepApplicationThemeToggle;
private CheckBox mFontCameraToggle;
private Spinner mLanguageSpinner;
private EditText mUnblurEdit;

Expand All @@ -79,6 +80,8 @@ public void onCreate(Bundle savedInstanceState) {
mShowPayPalActionBarIconToggle = (CheckBox) findViewById(R.id.show_paypal_action_bar_icon);
mKeepApplicationThemeToggle = (CheckBox) findViewById(R.id.keep_application_theme);

mFontCameraToggle = (CheckBox) findViewById(R.id.front_camera);

mLanguageSpinner = (Spinner) findViewById(R.id.language);
mUnblurEdit = (EditText) findViewById(R.id.unblur);

Expand Down Expand Up @@ -116,6 +119,7 @@ public void onScan(View pressed) {
.putExtra(CardIOActivity.EXTRA_LANGUAGE_OR_LOCALE, (String) mLanguageSpinner.getSelectedItem())
.putExtra(CardIOActivity.EXTRA_USE_PAYPAL_ACTIONBAR_ICON, mShowPayPalActionBarIconToggle.isChecked())
.putExtra(CardIOActivity.EXTRA_KEEP_APPLICATION_THEME, mKeepApplicationThemeToggle.isChecked())
.putExtra(CardIOActivity.EXTRA_FRONT_CAMERA, mFontCameraToggle.isChecked())
.putExtra(CardIOActivity.EXTRA_GUIDE_COLOR, Color.GREEN)
.putExtra(CardIOActivity.EXTRA_SUPPRESS_CONFIRMATION, mSuppressConfirmationToggle.isChecked())
.putExtra(CardIOActivity.EXTRA_SUPPRESS_SCAN, mSuppressScanToggle.isChecked())
Expand Down
6 changes: 6 additions & 0 deletions SampleApp/src/main/res/layout/sample_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@
android:layout_height="wrap_content"
android:text="Keep application theme"/>

<CheckBox
android:id="@+id/front_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open front camera"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
6 changes: 6 additions & 0 deletions card.io/src/main/java/io/card/payment/CardIOActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ public final class CardIOActivity extends Activity {
*/
public static final String EXTRA_KEEP_APPLICATION_THEME = "io.card.payment.keepApplicationTheme";

/**
* Boolean extra. Optional. If this value is set to <code>true</code>, the front camera will be opened.
* If this value is <code>false</code> (default), the back camera will be opened.
*/
public static final String EXTRA_FRONT_CAMERA = "io.card.payment.frontCamera";


/**
* Boolean extra. Used for testing only.
Expand Down
11 changes: 8 additions & 3 deletions card.io/src/main/java/io/card/payment/CardScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ private native void nScanFrame(byte[] data, int frameWidth, int frameHeight, int
private int numManualTorchChange;
private int numFramesSkipped;

private boolean mFrontCamera = false;

// ------------------------------------------------------------------------
// STATIC INITIALIZATION
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -196,6 +198,7 @@ static boolean processorSupported() {
mScanExpiry = scanIntent.getBooleanExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, false)
&& scanIntent.getBooleanExtra(CardIOActivity.EXTRA_SCAN_EXPIRY, true);
mUnblurDigits = scanIntent.getIntExtra(CardIOActivity.EXTRA_UNBLUR_DIGITS, DEFAULT_UNBLUR_DIGITS);
mFrontCamera = scanIntent.getBooleanExtra(CardIOActivity.EXTRA_FRONT_CAMERA, false);
}
mScanActivityRef = new WeakReference<>(scanActivity);
mFrameOrientation = currentFrameOrientation;
Expand All @@ -211,9 +214,11 @@ private Camera connectToCamera(int checkInterval, int maxTimeout) {
if (useCamera) {
do {
try {
// Camera.open() will open the back-facing camera. Front cameras are not
// attempted.
return Camera.open();
if (mFrontCamera) {
return Util.openFrontCamera();
} else {
return Util.openBackCamera();
}
} catch (RuntimeException e) {
try {
Log.w(Util.PUBLIC_LOG_TAG,
Expand Down
35 changes: 34 additions & 1 deletion card.io/src/main/java/io/card/payment/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ private static boolean hardwareSupportCheck() {
// Camera needs to open
Camera c = null;
try {
c = Camera.open();
// For checking the hardware, we open the default camera: the back one.
c = openBackCamera();
} catch (RuntimeException e) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return true;
Expand Down Expand Up @@ -113,6 +114,38 @@ private static boolean hardwareSupportCheck() {
return true;
}

/**
* Open the back camera
* @return the back camera opened
*/
static Camera openBackCamera() {
return Camera.open();
}

/**
* Open the front camera
* Inspired from https://stackoverflow.com/questions/2779002/how-do-i-open-the-front-camera-on-the-android-platform
* @return the front camera opened
*/
static Camera openFrontCamera() {
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}

return cam;
}

public static String getNativeMemoryStats() {
return "(free/alloc'd/total)" + Debug.getNativeHeapFreeSize() + "/"
+ Debug.getNativeHeapAllocatedSize() + "/" + Debug.getNativeHeapSize();
Expand Down