Java Question Trying to alter and recompile the Camera2 library

CyberDroidWare

Member
Licensed User
Hi all,

My goal is to alter and recompile the Camera2 library here: https://github.com/AnywhereSoftware...src/anywheresoftware/b4a/objects/Camera2.java

1. I have successfully downloaded and are using the B4J_LibraryCompiler.
2. I have successfully downloaded B4J.
3. I have successfully compiled and tested the FirstLibrary example - however note that it does not have dependency libraries.
4. In order for me to recreate Camera2 from above, naturally, I have to satisfy the following lines in the code block below.
5. Can somebody advise exactly, what files I need to put in the /libs directory, and where do I get these files from - so I can compile this with B4J_LibraryCompiler?

Naturally without any libs, I get this error.


Starting step: Compiling Java code.
javac 1.8.0_251
C:\Users\ss\Desktop\SimpleLibraryCompiler\Camera3\src\b4x\example\Camera2.java:25: error: package android.app does not exist
import android.app.Activity;
^
1 error
Error.



Dependencies - what do I put in libs?:
package anywheresoftware.b4a.objects;

import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.hardware.camera3.CameraAccessException;
import android.hardware.camera3.CameraCaptureSession;
import android.hardware.camera3.CameraCharacteristics;
import android.hardware.camera3.CameraDevice;
import android.hardware.camera3.CameraManager;
import android.hardware.camera3.CaptureRequest;
import android.hardware.camera3.CameraCaptureSession.CaptureCallback;
import android.hardware.camera3.CameraDevice.StateCallback;
import android.hardware.camera3.params.StreamConfigurationMap;
import android.media.Image;
import android.media.ImageReader;
import android.media.MediaCodec;
import android.media.MediaRecorder;
import android.util.Size;
import android.view.Surface;
import android.view.TextureView;
import anywheresoftware.b4a.AbsObjectWrapper;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.keywords.Common;
import anywheresoftware.b4a.objects.collections.List;
 

Star-Dust

Expert
Licensed User
Longtime User
I believe he wants to create a windings with b4j_compiler tool

you have to follow this tutorial
 

DonManfred

Expert
Licensed User
Longtime User
Please note that

B4J_LibraryCompiler.exe is used to compile B4J Libraries.
LibraryCompiler.exe is used to compile B4A Libraries.

To recompile Camera2 library, which is a B4A Lib, one need to use LibraryCompiler, not B4J_LibraryCompiler.
 

CyberDroidWare

Member
Licensed User
Please note that

B4J_LibraryCompiler.exe is used to compile B4J Libraries.
LibraryCompiler.exe is used to compile B4A Libraries.

To recompile Camera2 library, which is a B4A Lib, one need to use LibraryCompiler, not B4J_LibraryCompiler.
Gotya, interesting fact, the example I mentioned in my original post actually worked on compiling a B4A library!

I'm referring to FirstLibrary example provided in the B4J compiler download. It worked and compiled on an Android device, with B4A.
 

CyberDroidWare

Member
Licensed User
Ok thanks guys, so back to my original question, what goes in the .lib folder prior to compiling? I tried to simply replicate the exact Camera2 library

1. I placed the above Camera2.java file in a structure labelled c2/src/Camera2.java
2. I have created an empty lib folder in c2/lib/
3. I am using the LibraryCompiler.exe now
4. I am not changing the class name, I am happy to keep it as Camera2
5. In LibraryCompiler.exe the Project Folder goes to: c:\users\ss\desktop\c2
6. The Library Name remains: Camera2
7. I have left the -b4aignore as: org,com.android,com.example,com.hoho
5. When I press Compile, LibraryCompiler.exe compiles with 50 warnings

Starting step: Compiling Java code.
javac 11.0.1

Completed successfully.
Starting step: Creating jar file.
Completed successfully.
Starting step: Creating XML file.
Loading source file C:\Users\ss\Desktop\c2\src\Camera2.java...
Constructing Javadoc information...
[-doclet, BADoclet]
[-docletpath, C:\Users\ss\Desktop\SimpleLibraryCompiler]
[-doclet, BADoclet]
[-docletpath, C:\Users\ss\Desktop\SimpleLibraryCompiler]
[-bootclasspath, C:\Android\platforms\android-28\android.jar]
[-classpath, C:\Program Files (x86)\Anywhere Software\Basic4android\B4A.exe\../libraries\B4AShared.jar;C:\Program Files (x86)\Anywhere Software\Basic4android\B4A.exe\../libraries\Core.jar;]
[-sourcepath, src]
[-b4atarget, C:\Users\ss\Desktop\AdditionalLibraries\Camera2.xml]
[-b4aignore, org,com.android,com.example,com.hoho]
Ignoring: [org, com.android, com.example, com.hoho]
starting....
Working with class: anywheresoftware.b4a.objects.Camera2
Working with class: anywheresoftware.b4a.objects.Camera2.CameraSizeWrapper
finish: C:\Users\ss\Desktop\AdditionalLibraries\Camera2.xml
50 warnings

Completed successfully.
*** Don't forget to refresh the libraries list in the IDE (right click and choose Refresh) ***

6. I try now to compile my Android Camera2 example, and with the new library, Camera2 does not work and gives me below. I'm sure I would probably need to have dependency files in the /c2/lib folder?

B4A Version: 10.0
Java Version: 11
Parsing code. (0.02s)
Building folders structure. (0.04s)
Compiling code. (0.14s)
Compiling layouts code. (0.03s)
Organizing libraries. (0.00s)
(AndroidX SDK)
Generating R file. (0.27s)
Compiling debugger engine code. (4.00s)
Compiling generated Java code. Error
B4A line: 115
Camera.StartSession(tv, PreviewSize, CaptureSize,
src\b4a\example3\camex2.java:725: error: cannot find symbol
__ref._camera /*anywheresoftware.b4a.objects.Camera2*/ .StartSession((TextureView)(__ref._tv /*anywheresoftware.b4a.objects.ConcreteViewWrapper*/ .getObject()),__ref._previewsize /*anywheresoftware.b4a.objects.Camera2.CameraSizeWrapper*/ ,__ref._capturesize /*anywheresoftware.b4a.objects.Camera2.CameraSizeWrapper*/ ,(int) (0),(int) (0),parent.__c.True);
^
symbol: class TextureView
location: class ResumableSub_PrepareSurfaceForVideo

7. For what its worth, line 115 is: Camera.StartSession(tv, PreviewSize, CaptureSize, 0, 0, True)
 

Johan Schoeman

Expert
Licensed User
Longtime User
Have you tried to run the Jetifier and cleaned the project after you have compiled the Java code and before you compile the B4A project?

Looking at the Java code it does not seem to have any dependencies and will therefore not require the libs folder.

".so" files go in the "additional/lib/xxxxx" folders of the Java project.
 

CyberDroidWare

Member
Licensed User
Hi and thanks,

I tried this and this is what it said.

WARNING: [Main] No references were rewritten. You don't need to run Jetifier.

The file is identical to the first compiled .jar file. Is it not working due to the 50 warnings as above?

My goal is simply just to recompile Camera2.
S
 

CyberDroidWare

Member
Licensed User
I still get this error when I try to compile a B4A project that relies on my new Camera2.

Compiling generated Java code. Error
B4A line: 115
Camera.StartSession(tv, PreviewSize, CaptureSize,
src\b4a\example3\camex2.java:725: error: cannot find symbol
__ref._camera /*anywheresoftware.b4a.objects.Camera2*/ .StartSession((TextureView)(__ref._tv /*anywheresoftware.b4a.objects.ConcreteViewWrapper*/ .getObject()),__ref._previewsize /*anywheresoftware.b4a.objects.Camera2.CameraSizeWrapper*/ ,__ref._capturesize /*anywheresoftware.b4a.objects.Camera2.CameraSizeWrapper*/ ,(int) (0),(int) (0),parent.__c.True);
^
symbol: class TextureView
location: class ResumableSub_PrepareSurfaceForVideo
 

CyberDroidWare

Member
Licensed User
Hi all,
Can someone maybe compile a special version of Camera2 with just this change, or advise me why I get 50 warnings and can't replace it?
I think this would benefit everyone...

I need to open up this function as so, and replace it below:

public Object CreateMediaRecorder(CameraSizeWrapper VideoSize, String Dir, String FileName, int audioSource, int videoSource, int outputFormat, int bitRate, int frameRate, int frameWidth, int frameHeight, int videoEncoder, int audioEncoder ) {
if (mediaRecorder != null)
mediaRecorder.release();
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(audioSource);
mediaRecorder.setVideoSource(videoSource);
mediaRecorder.setOutputFormat(outputFormat);
mediaRecorder.setOutputFile(anywheresoftware.b4a.objects.streams.File.Combine(Dir, FileName));
mediaRecorder.setVideoEncodingBitRate(bitRate);
mediaRecorder.setVideoFrameRate(frameRate);
mediaRecorder.setVideoSize(frameWidth, frameHeight);
mediaRecorder.setVideoEncoder(videoEncoder);
mediaRecorder.setAudioEncoder(audioEncoder);

return mediaRecorder;
}
 

agraham

Expert
Licensed User
Longtime User
You could try reverting to using Java 8 in B4A. I have had problems making libraries with Java 11. I've tried to find the post where Erel advised me to go back to Java 8 but I can't but if I remember correctly under Java 11 full type names in the xml were truncated so the Java compiler couldn't find them. Looking at the error I think the cast

(TextureView)

is not a complete type name, it should probably be

(android.view.TextureView )

I think BADoclet needs some changes to run under Java 11 and 14.

EDIT: Corrected type name
 
Last edited:

CyberDroidWare

Member
Licensed User
Hmmm thanks I'll try that. I could do so much if I could just recompile this existing Camera2 library. Erel, if you're around, can you see what is going on here?
 

CyberDroidWare

Member
Licensed User
I still get this error when I try to compile a B4A project that relies on my new Camera2.

Compiling generated Java code. Error
B4A line: 115
Camera.StartSession(tv, PreviewSize, CaptureSize,
src\b4a\example3\camex2.java:725: error: cannot find symbol
__ref._camera /*anywheresoftware.b4a.objects.Camera2*/ .StartSession((TextureView)(__ref._tv /*anywheresoftware.b4a.objects.ConcreteViewWrapper*/ .getObject()),__ref._previewsize /*anywheresoftware.b4a.objects.Camera2.CameraSizeWrapper*/ ,__ref._capturesize /*anywheresoftware.b4a.objects.Camera2.CameraSizeWrapper*/ ,(int) (0),(int) (0),parent.__c.True);
^
symbol: class TextureView
location: class ResumableSub_PrepareSurfaceForVideo
You could try reverting to using Java 8 in B4A. I have had problems making libraries with Java 11. I've tried to find the post where Erel advised me to go back to Java 8 but I can't but if I remember correctly under Java 11 full type names in the xml were truncated so the Java compiler couldn't find them. Looking at the error I think the cast

(TextureView)

is not a complete type name, it should probably be

(android.widget.TextView )

I think BADoclet needs some changes to run under Java 11 and 14.
Hi, this appears normal as it is in the source code. It is really weird why I can't simply recompile with LibraryCompiler?
 

agraham

Expert
Licensed User
Longtime User
Hi, this appears normal as it is in the source code. It is really weird why I can't simply recompile with LibraryCompiler?
What appears normal? Which source code, library, or transpiled B4A code? You can argue against my suggestion once you have tried it.
 

DonManfred

Expert
Licensed User
Longtime User

CyberDroidWare

Member
Licensed User
Hi, I refer to the source code directly in the Camera2.java file, "https://github.com/AnywhereSoftware...src/anywheresoftware/b4a/objects/Camera2.java".

TextureView:
public void StartSession(TextureView Surface, CameraSizeWrapper PreviewSize, CameraSizeWrapper CaptureSize, int CaptureFormat, int PreviewFormat, boolean Video) throws CameraAccessException, IllegalStateException, IOException {
        if (captureSession != null) {
            captureSession.close();
            captureSession = null;
        }
        if (previewSurface != null)
            previewSurface.release();
        ArrayList<Surface> targets = new ArrayList<android.view.Surface>();
        if (Surface != null) {
            previewSurface = new Surface(Surface.getSurfaceTexture());
            Surface.getSurfaceTexture().setDefaultBufferSize(PreviewSize.getWidth(), PreviewSize.getHeight());
            targets.add(previewSurface);
        }
 

agraham

Expert
Licensed User
Longtime User
Sigh! The code that is erroring is not in the library but is the transpiled B4A Java code. The xml tells B4A the types of the parameters. The original Camera2 xml has this for the first parameter of StartSession. Note the type name.
XML:
           <name>StartSession</name>
            <comment>Starts a capture session. The SessionConfigured event will be raised.</comment>
            <returntype>void</returntype>
            <parameter>
                <name>Surface</name>
                <type>android.view.TextureView</type>
            </parameter>
From your error I suspect the type in your generated xml will be <type>TextureView</type> which is a problem I have experienced using BADoclet with Java 11.
 

CyberDroidWare

Member
Licensed User
Sigh! The code that is erroring is not in the library but is the transpiled B4A Java code. The xml tells B4A the types of the parameters. The original Camera2 xml has this for the first parameter of StartSession. Note the type name.
XML:
           <name>StartSession</name>
            <comment>Starts a capture session. The SessionConfigured event will be raised.</comment>
            <returntype>void</returntype>
            <parameter>
                <name>Surface</name>
                <type>android.view.TextureView</type>
            </parameter>
From your error I suspect the type in your generated xml will be <type>TextureView</type> which is a problem I have experienced using BADoclet with Java 11.
Spot on, this is in my generated XML - <type>TextureView</type>

So the answer is to downgrade the java jdk to version 8 and point LibraryCompiler to that?
 
Top