Skip to content

Commit

Permalink
change callback
Browse files Browse the repository at this point in the history
  • Loading branch information
pqpo committed Sep 18, 2018
1 parent f41f642 commit c9a3c32
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 77 deletions.
13 changes: 9 additions & 4 deletions app/src/main/java/me/pqpo/smartcamera/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.cameraview.CameraView;
import com.google.android.cameraview.Camera1;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.Observer;
Expand Down Expand Up @@ -141,11 +141,16 @@ public boolean onScanResult(SmartCameraView smartCameraView, int result, byte[]
}
});

mCameraView.addCallback(new CameraView.Callback() {
mCameraView.addCallback(new Camera1.Callback() {

@Override
public void onPictureTaken(CameraView cameraView, byte[] data) {
super.onPictureTaken(cameraView, data);
public void onCameraOpened(Camera1 camera) {
super.onCameraOpened(camera);
}

@Override
public void onPictureTaken(Camera1 camera, byte[] data) {
super.onPictureTaken(camera, data);
mCameraView.cropImage(data, new SmartCameraView.CropCallback() {
@Override
public void onCropped(Bitmap cropBitmap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.util.SortedSet;
import java.util.concurrent.atomic.AtomicBoolean;

class Camera1 {
public class Camera1 {

private static final int INVALID_CAMERA_ID = -1;

Expand Down Expand Up @@ -120,7 +120,7 @@ void startPreview() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (previewBuffer != null && mCallback != null) {
mCallback.onPicturePreview(previewBuffer);
mCallback.onPicturePreview(Camera1.this, previewBuffer);
if (mCamera != null) {
mCamera.addCallbackBuffer(previewBuffer);
}
Expand Down Expand Up @@ -315,7 +315,7 @@ void takePictureInternal() {

public void onPictureTaken(byte[] data, Camera camera) {
isPictureCaptureInProgress.set(false);
mCallback.onPictureTaken(data);
mCallback.onPictureTaken(Camera1.this, data);
camera.cancelAutoFocus();
startPreview();
}
Expand Down Expand Up @@ -378,7 +378,7 @@ private void openCamera() {
}
adjustCameraParameters();
mCamera.setDisplayOrientation(calcDisplayOrientation(mDisplayOrientation));
mCallback.onCameraOpened();
mCallback.onCameraOpened(this);
}

private AspectRatio chooseAspectRatio() {
Expand Down Expand Up @@ -448,7 +448,7 @@ private void releaseCamera() {
if (mCamera != null) {
mCamera.release();
mCamera = null;
mCallback.onCameraClosed();
mCallback.onCameraClosed(this);
}
}

Expand Down Expand Up @@ -549,15 +549,15 @@ private boolean setFlashInternal(int flash) {
}
}

interface Callback {
public abstract static class Callback {

void onCameraOpened();
public void onCameraOpened(Camera1 camera){}

void onCameraClosed();
public void onCameraClosed(Camera1 camera){}

void onPictureTaken(byte[] data);
public void onPictureTaken(Camera1 camera, byte[] data){}

void onPicturePreview(byte[] data);
public void onPicturePreview(Camera1 camera, byte[] data){}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public void onDisplayOrientationChanged(int displayOrientation) {
};
}

public Camera1 getCamera() {
return mImpl;
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Expand Down Expand Up @@ -267,21 +271,15 @@ public boolean isCameraOpened() {

/**
* Add a new callback.
*
* @param callback The {@link Callback} to add.
* @see #removeCallback(Callback)
*/
public void addCallback(@NonNull Callback callback) {
public void addCallback(@NonNull Camera1.Callback callback) {
mCallbacks.add(callback);
}

/**
* Remove a callback.
*
* @param callback The {@link Callback} to remove.
* @see #addCallback(Callback)
*/
public void removeCallback(@NonNull Callback callback) {
public void removeCallback(@NonNull Camera1.Callback callback) {
mCallbacks.remove(callback);
}

Expand Down Expand Up @@ -398,58 +396,57 @@ public int getFlash() {

/**
* Take a picture. The result will be returned to
* {@link Callback#onPictureTaken(CameraView, byte[])}.
*/
public void takePicture() {
mImpl.takePicture();
}

private class CallbackBridge implements Camera1.Callback {
private class CallbackBridge extends Camera1.Callback {

private final ArrayList<Callback> mCallbacks = new ArrayList<>();
private final ArrayList<Camera1.Callback> mCallbacks = new ArrayList<>();

private boolean mRequestLayoutOnOpen;

CallbackBridge() {
}

public void add(Callback callback) {
public void add(Camera1.Callback callback) {
mCallbacks.add(callback);
}

public void remove(Callback callback) {
public void remove(Camera1.Callback callback) {
mCallbacks.remove(callback);
}

@Override
public void onCameraOpened() {
public void onCameraOpened(Camera1 camera) {
if (mRequestLayoutOnOpen) {
mRequestLayoutOnOpen = false;
requestLayout();
}
for (Callback callback : mCallbacks) {
callback.onCameraOpened(CameraView.this);
for (Camera1.Callback callback : mCallbacks) {
callback.onCameraOpened(camera);
}
}

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

@Override
public void onPictureTaken(byte[] data) {
for (Callback callback : mCallbacks) {
callback.onPictureTaken(CameraView.this, data);
public void onPictureTaken(Camera1 camera, byte[] data) {
for (Camera1.Callback callback : mCallbacks) {
callback.onPictureTaken(camera, data);
}
}

@Override
public void onPicturePreview(byte[] data) {
for (Callback callback : mCallbacks) {
callback.onPicturePreview(CameraView.this, data);
public void onPicturePreview(Camera1 camera, byte[] data) {
for (Camera1.Callback callback : mCallbacks) {
callback.onPicturePreview(camera, data);
}
}

Expand Down Expand Up @@ -509,39 +506,4 @@ public SavedState[] newArray(int size) {

}

/**
* Callback for monitoring events about {@link CameraView}.
*/
@SuppressWarnings("UnusedParameters")
public abstract static class Callback {

/**
* Called when camera is opened.
*
* @param cameraView The associated {@link CameraView}.
*/
public void onCameraOpened(CameraView cameraView) {
}

/**
* Called when camera is closed.
*
* @param cameraView The associated {@link CameraView}.
*/
public void onCameraClosed(CameraView cameraView) {
}

/**
* Called when a picture is taken.
*
* @param cameraView The associated {@link CameraView}.
* @param data JPEG data.
*/
public void onPictureTaken(CameraView cameraView, byte[] data) {
}

public void onPicturePreview(CameraView cameraView, byte[] data) {
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.google.android.cameraview.Camera1;
import com.google.android.cameraview.CameraView;
import com.google.android.cameraview.base.Size;

Expand Down Expand Up @@ -46,10 +47,10 @@ private void init() {
smartScanner = new SmartScanner();
uiHandler = new ScanResultHandler(this);

addCallback(new Callback() {
addCallback(new Camera1.Callback() {
@Override
public void onPicturePreview(CameraView cameraView, byte[] data) {
super.onPicturePreview(cameraView, data);
public void onPicturePreview(Camera1 camera, byte[] data) {
super.onPicturePreview(camera, data);
if (data == null || !scanning) {
return;
}
Expand Down

0 comments on commit c9a3c32

Please sign in to comment.