Android Question Camera2 Library

walterf25

Expert
Licensed User
Longtime User
Hi all, i'm working on an app at work to test the cameras on a product we are developing, i was using the CameraEx class, but read somewhere that it was recommended to use the Camera2 library, i am able to deploy the apk just fine and am also able to see the camera preview, the issue i just noticed is that when I use the B4A Camera2 example found in the forums i noticed that the image looks a bit distorted and it also has this purplish layer on top, i decided to write another app using Android Studio using the Camera2 API and i realized that using Android Studio the image looks normal, the image doesn't look pixelated and the colors look natural, from what i can see by going through the code on both the B4A example and the Android Studio app code i really don't see a difference, but obviously there is a setting or something I am probably missing.

i am attaching both images, the first one is taken from the B4A example app and the second one is taken using the Android Studio App.

B4A_.1PNG.png
Android_Studio2.png

Any help figuring out why there is a difference will be greatly appreciated, I work for SnapChat, Yes the SnapChat app company and we are developing some exciting products, the software engineers here are very impressed by what can be accomplished with B4A, i have been assigned to develop a test app to be able to test the cameras, wifi module, bluetooth module and a few other pieces of hardware and i would really love to figure out this particular issue i am seeing, so any and all help will be very much appreciated.

i am also attaching the source code for the Android Studio project just in case someone cares to take a look and maybe help me figure out what the difference is.

Erel's Camera2 example code can be found here.

Regards,
Walter
 

Attachments

  • cameraapp.zip
    4.3 KB · Views: 433

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. First step is to decide whether you should use Camera or Camera2.

What is the output of Log(CamEx2.SupportedHardwareLevel) ?

2. The distortion is related to the preview panel size vs. the preview frame size. You should either change the panel size or the preview size.

I'm not sure what causes the color issue. You will need to test it with different parameters.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
1. First step is to decide whether you should use Camera or Camera2.
What is the output of Log(CamEx2.SupportedHardwareLevel) ?
the output of that CamEx2.SupportedHardwareLevel is FULL

2. The distortion is related to the preview panel size vs. the preview frame size. You should either change the panel size or the preview size.

i looked at the Android Studio code, specifically the createCameraPreview function and i am setting the panel size preview size to the same as in the Android Studio code, which is 1748 width and 1748 height, this works just fine with the AS code.
B4X:
            protected void createCameraPreview () {
                try {
                    SurfaceTexture texture = textureView.getSurfaceTexture();
                    assert texture != null;
                    Log.d(TAG, "width: " + imageDimension.getWidth() + " " + "height: " + imageDimension.getHeight());
                    texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
                    Surface surface = new Surface(texture);
                    captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                    captureRequestBuilder.addTarget(surface);
                    manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
                    characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
                    List<CaptureRequest.Key<?>> reqs = new ArrayList<CaptureRequest.Key<?>>();
                    reqs =  characteristics.getAvailableCaptureRequestKeys(); //characteristics.getAvailableCaptureRequestKeys();
                    for (int i=0; i<reqs.size(); i++) {
                        CaptureRequest.Key<?> reqss;
                        // Log.d(TAG, "captureRequestKey: " + reqs.get(i));
                        reqss = reqs.get(i);
                        Log.d(TAG, "KEY: " + reqss.getName() + " " + "VALUE " + captureRequestBuilder.build().get(reqss));
                        // Log.d(TAG, "VALUE: " + reqss.
                    }
                    cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
                        @Override
                        public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                            //The camera is already closed
                            if (null == cameraDevice) {
                                return;
                            }
                            // When the session is ready, we start displaying the preview.
                            cameraCaptureSessions = cameraCaptureSession;
                            updatePreview();
                        }

                        @Override
                        public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                            Toast.makeText(MainActivity.this, "Configuration change", Toast.LENGTH_SHORT).show();
                        }
                    }, null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }

I'm not sure what causes the color issue. You will need to test it with different parameters.
I'm not sure about the color issue either but i need to figure this out, i am really trying to prove to them that we can use B4A without any limitations here at least to write apps for the testing side of it.

Thanks Erel, please let me know if you have any suggestions i can try, i'm not sure what the difference between Camera and Camera2 are, but as i mentioned before i am using the Camera2 API in Android Studio and that works just the way it should.

Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Ignore the Android Studio code.

The camera2 example sets the preview size to:
B4X:
cam.PreviewSize.Initialize(1920, 1080)

You need to make sure that the pnlCamera width / height ratio is set to be the same. Otherwise it will be distorted.
Hi Erel, i've already done that, i have made sure that the cam.PreviewSize and the pnlCamera.width/pnlCamera.height have the same ratio, the image looks better now, but I still see the purple layer on top, what parameters would you suggest i play around with to see if I can get rid of this issue?

Thanks,
Walter
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
i am attaching both images, the first one is taken from the B4A example app and the second one is taken using the Android Studio App.
I think I've seen that before...seems a problem between the different allowed preview formats YUV_422, YUV_420, or others, in which chroma information is interleaved in different ways in the output array. The visual effect when this happens is the one in the picture you attached.
While most of the cameras have a default preview format, it is an error to assume all of them use the same. I guess that the android Studio project takes this into account but should be explicitly checked/added in the camera2 example. Just my guess
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I think I've seen that before...seems a problem between the different allowed preview formats YUV_422, YUV_420, or others, in which chroma information is interleaved in different ways in the output array. The visual effect when this happens is the one in the picture you attached.
While most of the cameras have a default preview format, it is an error to assume all of them use the same. I guess that the android Studio project takes this into account but should be explicitly checked/added in the camera2 example. Just my guess
Hi JordiCp for your insight, I actually got it working just fine yesterday and it seems you are right about the different preview formats, all i did was choose a different preview size and that got rid of the pinkish layer color on top, by default the 1734x1734 preview size was selected, i played around with a few different sizes until finally the 800x400 preview size worked just fine.

Thanks everyone for your help.

Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I think I've seen that before...seems a problem between the different allowed preview formats YUV_422, YUV_420, or others, in which chroma information is interleaved in different ways in the output array. The visual effect when this happens is the one in the picture you attached.
While most of the cameras have a default preview format, it is an error to assume all of them use the same. I guess that the android Studio project takes this into account but should be explicitly checked/added in the camera2 example. Just my guess
I forgot to mention that I went back to using the original native camera class instead of the camera2 api, the preview looks just great now, but I just realized that when I take a picture the picture comes out very dark and I can still see the pinkish/purplish layer on top, any ideas as to why this would be?

Regards,
Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Try it with different auto exposure modes (see the example).


Have you changed the CaptureSize value?
Hi @Erel, I switched back to the CameraEx class using the native Camera API, i am not using the Camera2 class anymore, this seems to show the preview picture just fine, with the right colors etc. but when I take a picture that's when the picture taken comes out very dark and with the pinkish/purplish layer on top, is there a way to set the capturesize using the original camera api class? I don't see a method for that, also i don't see a way to set the auto exposure modes using the native camera api.

Thanks,
Walter
 
Upvote 0
Top