Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.
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 @@ -247,6 +247,13 @@ public void onCameraClosed(CameraView cameraView) {
Log.d(TAG, "onCameraClosed");
}

@Override
public void onCameraNotAvailable(CameraView cameraView) {
Log.d(TAG, "onCameraNotAvailable");
Toast.makeText(cameraView.getContext(), R.string.camera_not_available,
Toast.LENGTH_LONG).show();
}

@Override
public void onPictureTaken(CameraView cameraView, final byte[] data) {
Log.d(TAG, "onPictureTaken " + data.length);
Expand Down
1 change: 1 addition & 0 deletions demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
<string name="flash_auto">Flash auto</string>
<string name="flash_off">Flash off</string>
<string name="flash_on">Flash on</string>
<string name="camera_not_available">There are no cameras available.</string>
</resources>
26 changes: 23 additions & 3 deletions library/src/main/api14/com/google/android/cameraview/Camera1.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public void onSurfaceChanged() {
@Override
boolean start() {
chooseCamera();
openCamera();
if (!openCamera()) {
return false;
}

if (mPreview.isReady()) {
setUpPreview();
}
Expand Down Expand Up @@ -285,11 +288,27 @@ private void chooseCamera() {
mCameraId = INVALID_CAMERA_ID;
}

private void openCamera() {
private boolean openCamera() {
if (mCamera != null) {
releaseCamera();
}
mCamera = Camera.open(mCameraId);

mCamera = null;
if (mCameraId == INVALID_CAMERA_ID) {
mCallback.onCameraNotAvailable();
return false;
}

try {
mCamera = Camera.open(mCameraId);
}
catch(RuntimeException ex) {
mCamera = null;
mCallback.onCameraNotAvailable();
return false;
}

assert(mCamera != null);
mCameraParameters = mCamera.getParameters();
// Supported preview sizes
mPreviewSizes.clear();
Expand All @@ -308,6 +327,7 @@ private void openCamera() {
adjustCameraParameters();
mCamera.setDisplayOrientation(calcCameraRotation(mDisplayOrientation));
mCallback.onCameraOpened();
return true;
}

private AspectRatio chooseAspectRatio() {
Expand Down
12 changes: 10 additions & 2 deletions library/src/main/api21/com/google/android/cameraview/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,12 @@ private boolean chooseCameraIdByFacing() {
int internalFacing = INTERNAL_FACINGS.get(mFacing);
final String[] ids = mCameraManager.getCameraIdList();
if (ids.length == 0) { // No camera
throw new RuntimeException("No camera available.");
mCameraId = null;
Log.e(TAG, "No camera devices present.");
mCallback.onCameraNotAvailable();
return false;
}

for (String id : ids) {
CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(id);
Integer level = characteristics.get(
Expand Down Expand Up @@ -458,9 +462,13 @@ private void prepareImageReader() {
* <p>The result will be processed in {@link #mCameraDeviceCallback}.</p>
*/
private void startOpeningCamera() {
if (mCameraId == null) {
return;
}

try {
mCameraManager.openCamera(mCameraId, mCameraDeviceCallback, null);
} catch (CameraAccessException e) {
} catch (SecurityException|CameraAccessException e) {
throw new RuntimeException("Failed to open camera: " + mCameraId, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ interface Callback {

void onCameraClosed();

void onCameraNotAvailable();

void onPictureTaken(byte[] data);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,13 @@ public void onCameraClosed() {
}
}

@Override
public void onCameraNotAvailable() {
for (Callback callback : mCallbacks) {
callback.onCameraNotAvailable(CameraView.this);
}
}

@Override
public void onPictureTaken(byte[] data) {
for (Callback callback : mCallbacks) {
Expand Down Expand Up @@ -527,6 +534,14 @@ public void onCameraOpened(CameraView cameraView) {
public void onCameraClosed(CameraView cameraView) {
}

/**
* Called when there is no camera to open
*
* @param cameraView The associated {@link CameraView}.
*/
public void onCameraNotAvailable(CameraView cameraView) {
}

/**
* Called when a picture is taken.
*
Expand Down