Android Question CamEx Front Camera is stretching

Alexander Stolte

Expert
Licensed User
Longtime User
Hello,

how can i get the best resolution for the front camera? I'm using the "CamEx" lib.

If i call "GetSupportedPreviewSizes" then i have all sizes, but not only for the front camera. If i set the minium resolution, then the preview image is stretching.
 

JordiCP

Expert
Licensed User
Longtime User
Hi,

GetSupportedPreviewSizes will tell you the supported preview sizes of the current camera index (usually 0:rear, 1:front, but not necessarily in all devices)

Each device screen has a different aspect ratio, so if for instance you want always fullScreen, you'll have to "cut" a part of the image that will be off-screen (this is what some apps do). Or also you'll need a 3:1 aspect ratio for the preview, the strategy changes --> but always panel aspect ratio has to be related to the preview aspect rati. Otherwise, of course the stretching will always happen.

So, the idea is
  • Get a list of the supported preview sizes for the used camera
  • If you are not going to process the preview, then the total resolution (width*height) is not an issue. Otherwise, it will define a subset of all the available resolutions.
  • Decide which strategy you want:
    • If full-screen -> search for those supported preview sizes with the same aspect ratio as your screen (or as-near-as-possible). Dim the preview Panel accordingly and place it centered with on of its dimensions "off-screen"
    • With a fixed aspect-ratio (for instance 3:2) --> Search for those preview resolutions with this aspect ratio. Dim the panel to be 100%X width (assuming the app is in portrait mode) and Heigh accordingly
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Thanks for your reply, i use your example here to calculate the best resolution for fullscreen. The Problem is, that i cant take this resolution for the front camera, because the commit parameters run in to a error:
B4X:
Error occurred on line: 162 (CameraExClass)
java.lang.RuntimeException: setParameters failed
    at android.hardware.Camera.native_setParameters(Native Method)
    at android.hardware.Camera.setParameters(Camera.java:2369)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:216)
    at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod4(Reflection.java:857)
    at anywheresoftware.b4a.samples.camera2.cameraexclass._commitparameters(cameraexclass.java:141)
    at anywheresoftware.b4a.samples.camera2.cameraexclass._setdisplayorientation(cameraexclass.java:863)
    at anywheresoftware.b4a.samples.camera2.cameraexclass._camera_ready(cameraexclass.java:765)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:360)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at anywheresoftware.b4a.objects.CameraW$2$1.run(CameraW.java:139)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6753)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
if I take the normal resolution, then I get a stretched picture.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
That example was just to illustrate a possible solution to an issue.

I think I see the problem you report --> The inline Java code was only executed once at the start and was only valid for the rear camera. So, if you execute it, then switch to the front camera, and it results that the chosen optimal resolution (for the back camera) is not in the set of supported preview resolutions for the front camera, it will crash.
If you want it to work for both, back and front, cameras, you should call the inline Java part twice, each one with the correct index (added parameter and modified line) and keep the 'optimal' resolutions in different vars for front and back camera

B4X:
public void calculateOptimalSizes(int cameraIndex, int w,int h,int[] previewSize, int[] pictureSize){

    float mAspectRatio = (float)w/h;
    if (mAspectRatio<1f) mAspectRatio = 1f/mAspectRatio;
    int mSize = w*h;

    BA.Log("Aspect Ratio is: "+mAspectRatio);

    //Camera mCam = Camera.open(0);  //<-- this was ONLY for back camera
    Camera mCam = Camera.open(cameraIndex);    //<-- this is for a given camera index

    Camera.Parameters mParams = mCam.getParameters();
    // ...
}
 
Last edited:
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Hey, thank you very mutch! But what do you mean with that? Where do I get the index from?
Just pass an Int which is either 0 (back camera) or 1 (front camera) to the the linline Java code in post #6 above.
 
Upvote 0
Top