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

Fix camera preview not showing on first launch #144

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 11 additions & 11 deletions card.io/src/main/java/io/card/payment/CardScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CardScanner implements Camera.PreviewCallback, Camera.AutoFocusCallback,
private native void nGetGuideFrame(int orientation, int previewWidth, int previewHeight, Rect r);

private native void nScanFrame(byte[] data, int frameWidth, int frameHeight, int orientation,
DetectionInfo dinfo, Bitmap resultBitmap, boolean scanExpiry);
DetectionInfo dinfo, Bitmap resultBitmap, boolean scanExpiry);

private native int nGetNumFramesScanned();

Expand Down Expand Up @@ -106,7 +106,7 @@ private native void nScanFrame(byte[] data, int frameWidth, int frameHeight, int
// accessed by test harness subclass.
protected boolean useCamera = true;

private boolean isSurfaceValid;
private boolean isPreviewInit;

private int numManualRefocus;
private int numAutoRefocus;
Expand Down Expand Up @@ -311,7 +311,7 @@ boolean resumeScanning(SurfaceHolder holder) {
mCamera.setPreviewCallbackWithBuffer(this);
}

if (isSurfaceValid) {
if (holder.getSurface().isValid()) {
makePreviewGo(holder);
}

Expand Down Expand Up @@ -355,26 +355,28 @@ public void endScanning() {
* --------------------------- SurfaceHolder callbacks
*/

private boolean makePreviewGo(SurfaceHolder holder) {
private void makePreviewGo(SurfaceHolder holder) {
// method name from http://www.youtube.com/watch?v=-WmGvYDLsj4
if (isPreviewInit) {
return;
}
assert holder != null;
assert holder.getSurface() != null;

isPreviewInit = true;
mFirstPreviewFrame = true;

if (useCamera) {
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
return false;
} catch (IOException ignore) {
}
try {
mCamera.startPreview();
mCamera.autoFocus(this);
} catch (RuntimeException e) {
return false;
} catch (RuntimeException ignore) {
}
}
return true;
}

/*
Expand All @@ -384,7 +386,6 @@ private boolean makePreviewGo(SurfaceHolder holder) {
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where to draw.
if (mCamera != null || !useCamera) {
isSurfaceValid = true;
makePreviewGo(holder);
} else {
Log.wtf(Util.PUBLIC_LOG_TAG, "CardScanner.surfaceCreated() - camera is null!");
Expand All @@ -411,7 +412,6 @@ public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(Util.PUBLIC_LOG_TAG, "error stopping camera", e);
}
}
isSurfaceValid = false;
}

/**
Expand Down