google / cameraview

[DEPRECATED] Easily integrate Camera features into your Android app
Apache License 2.0
4.74k stars 1.03k forks source link

Preview callback #41

Open liberorignanese opened 7 years ago

liberorignanese commented 7 years ago

it would be helpful if there was a callback for reading preview frame.

Carpetfizz commented 7 years ago

Any update on this? Is there no way to read frame data from the camera?

xCatG commented 7 years ago

Hi All, I'm trying to add support for accessing preview frames, my current thinking is in CameraViewImpl, add a interface like

interface FrameCallback {
    void onFrame(byte[] data, int format);
}

We can then add a mFrameCallback so each subclasses can deal with it. In Camera1, in openCamera we can call setPreviewCallback to supply us with the frame; in Camera2 we add an extra ImageReader that does the per-frame reading, and on its callback we can again call onFrame() with the byte from the Image object, as well as its format.

int format in the onFrame() call might be changed to a different object containing image info, not sure yet as I haven't start coding.

Where I am not sure is how to accept a external hook from CameraView itself. I am not sure how many people will want this behavior by default, so a separate CameraViewWithPreviewCall that extends CameraView might be more desirable? Also, given these are usually inflated from layout xml, the entry where we set callbacks might not be easy (to be seen, again, I haven't start coding yet).

Any thoughts, comments?

xCatG commented 7 years ago

Another approach is Add void onFrame(byte[] data, int format);' toCameraView.Callback, as well asCameraViewImpl.Callback. InCamera1, we call this in theonPreviewFrame(); inCamera2, this is called on aImageReader'sOnImageAvailableListener`.

@yaraki out of the two approaches, which one you prefer? Or you have other approaches that I should explore more about?

xCatG commented 7 years ago

The reason for including format as part of the callback is because Camera1 uses ImageFormat.NV21 or ImageFormat.YV12 and Camera2 recommends ImageFormat.YUV_420_888.

Haven't dig more, but we might have to also include image width and height, event stride value to correctly decode things. Still trying things out right now.

xCatG commented 7 years ago

PR #123 by @Sumsar is basically the second approach.

jackyhieu1211 commented 7 years ago

@xCatG Camera1 on device HTC Butterfly 4.3 API 18 not call mCamera.setPreviewCallback(new Camera.PreviewCallback() { @override public void onPreviewFrame(byte[] data, Camera camera) { Timber.e("onPreviewFrame"); final Camera.Parameters parameters = camera.getParameters(); final int width = parameters.getPreviewSize().width; final int height = parameters.getPreviewSize().height; mCallback.onPreviewFrame(data, parameters.getPreviewFormat(), width, height); } });