Android Question trying to access Google Play Services API via inline Java

Elton Leung

Member
Licensed User
Longtime User
I am quite new to B4A. Since I don't how to warp a library, I am trying to access the New Face detection API provided by Google Play Services via inline Java method. I did my homework and copy the latest google-play-services.jar to the AdditionalLibraries folder.

Then I added the manifest editor code:

B4X:
AddApplicationText(<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="face"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>)

I also added a reference to Google play resources at the top of the main code, at last I am basically copy other people's java sample into a java inline function. But my code gives 2 errors:

1. It looks like the compiler can't find the google play jar file:
Compiling generated Java code. Error
javac 1.8.0_60
src\com\greenbulb\google\face\detection\main.java:4: error: package com.google.android.gms.vision.face does not exist
import com.google.android.gms.vision.face.Face;
^
Note: src\com\greenbulb\google\face\detection\starter.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

**without the import com.google.android.gms.vision.face.Face; , compiler will say error: cannot find symbol: class FaceDetector

2. I can't pass the Bitmap variable from B4A into the inline Java.
Compiling generated Java code. Error
B4A line: 34
Dim s As String = NativeMe.RunMethod(\
javac 1.8.0_60
src\com\greenbulb\google\face\detection\main.java:329: error: incompatible types: Bitmap cannot be converted to Object[]
_s = BA.ObjectToString(_nativeme.RunMethod("FirstMethod",(Object[])(anywheresoftware.b4a.keywords.Common.LoadBitmap(anywheresoftware.b4a.keywords.Common.File.getDirAssets(),"image01.jpg").getObject())));

Looking like using Array can solve this problem.
^
Sorry, I am trying my best to solve the problem myself. But Java coding is way beyond my knowledge, this is why I try to code in B4A in the first place. May someone pls tell me the solution? Thanks in advance.


Here is my code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: Google face detection
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #AdditionalRes: C:\android\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private NativeMe As JavaObject
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    If FirstTime Then
     NativeMe.InitializeContext
    End If
    Dim s As String = NativeMe.RunMethod("FirstMethod", Array(LoadBitmap(File.DirAssets, "image01.jpg"),1))
    ToastMessageShow (s,True)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

#If JAVA
import android.graphics.Bitmap;
import com.google.android.gms.vision.face.Face;
import com.google.android.gms.vision.face.FaceDetector;
import com.google.android.gms.vision.Frame;

public String FirstMethod(Bitmap bitmap, Int a) {
    FaceDetector detector = new FaceDetector.Builder(getApplicationContext())
            .setTrackingEnabled(false)
            .build();

    // Create a frame from the bitmap and run face detection on the frame.
    Frame frame = new Frame.Builder().setBitmap(bitmap).build();
    SparseArray<Face> faces = detector.detect(frame);



    detector.release();
       return faces.size() + " faces detected";
}
#End If
 
Last edited:

Elton Leung

Member
Licensed User
Longtime User
You need to use #AdditionalJar to tell the compiler to add a reference to that jar.

https://www.b4x.com/android/forum/t...onaljar-and-javaobject-picasso.40904/#content


Thank you so much for the reply. Dexer has failed, it is normal? I have increase ram to 2048 already.

B4X:
B4A version: 5.20
Parsing code.    (0.00s)
Compiling code.    (0.16s)
Compiling layouts code.    (0.01s)
Generating R file.    (0.78s)
Compiling generated Java code.    (3.48s)
Convert byte code - optimized dex.    (56.41s)
    Optimized dexer failed. Switching to Standard dexer.
Packaging files.    (4.18s)
Copying libraries resources    (0.07s)
Signing package file (debug key).    (3.22s)
ZipAlign file.    (0.48s)
Installing file to device.    Error
No device found.
 
Upvote 0

Elton Leung

Member
Licensed User
Longtime User
Thanks!!!! Last question, this line is giving problem.

B4X:
import com.google.android.util.SparseArray;


Do I need to use #AdditionalJar for this too?
 
Upvote 0
Top