Java Question return list from Java wrapper

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I'm writing a wrapper to interface to a few of the functions of the OpenCV library by passing bitmaps rather than using their camera interface. So far I've got it doing things like thresholds and grayscales. The next step is to have it find some contours.

Contours are returned from the OpenCV function as a list of arrays of points. For example the returned list may contain five contours where each contour consists of a dozen or so x/y points.

Is it possible to return a Java list of objects from my wrapper to the B4A program and use it within B4A?

I'm thinking along the lines of creating a Type (it may already exist) for Point that would contain two floats, x and y. The wrapper would return a list of contours. In B4A I would assign this list to a B4A list. I would then get each object from the list. Each object would be a contour - an array of Points.

Am I close? Is there a different way I would need to return a list from a wrapper?

An alternative method would be to return each contour as a string array of CSV x and y points. I would iterate for the number of contours in the list.

If returning a list is not easy, would this alternative method be preferable?

Thanks,
Barry.
 

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks, that's great.

Just to repeat things to make sure I have them correct…

  • I place the code shown above in the Java wrapper?
  • I return the variable l1 from my wrapper to the B4A calling code?
  • This can be assigned in B4A to a B4A list (for example, to a list dimmed lst as List)?
  • The list objects will be contours which are arrays of float x,y points. If I create a Type Point(x, y as Float) in B4A, then declare a variable cont() as Point, can I do something in B4A like cont = lst.get(i) where cont will aquire an array of Points?
Thanks,
Barry.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
This can be assigned in B4A to a B4A list (for example, to a list dimmed lst as List)?
Yes. This is a regular B4A List object.

can I do something in B4A like cont = lst.get(i) where cont will aquire an array of Points?
No. You will need to expose a class in your library for that.
 

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
Thanks for your response in December of last year. I am finally getting to the point in my project where I need to return a list from Java library.

To make sure I understand your response I developed a small test library and small test B4A program.

The relevant code from the Java library:
B4X:
public class CRTlr {

    public String[] getStrings() {
        String str;
        String sRes[] = new String[5];
     
        for (int i=0; i<5; i++) {
            str = "This is String #" + i;
         
            sRes[i] = str;
        }
     
        return(sRes);
    }

    public anywheresoftware.b4a.objects.collections.List getList() {
        String str;
        List<Object> myList = new ArrayList<Object>();     
     
        for (int i=0; i<5; i++) {
            str = "This is List Item #" + i;
         
            myList.add(str);
        }
     
        anywheresoftware.b4a.objects.collections.List l1 = new anywheresoftware.b4a.objects.collections.List();
        l1.setObject(myList);
     
        return(l1);
    }

}

And the relevant code from B4A:
B4X:
Sub bnStr_Click
  Dim str() As String 

  str = CRTlr.Strings

  lbOut.Text = ""

  For i=0 To str.Length - 1
    lbOut.Text = lbOut.Text & str(i) & CRLF
  Next
End Sub

Sub bnLst_Click
  Dim mlst As List

  mlst = CRTlr.List

  lbOut.Text = ""

  For i=0 To mlst.Size - 1
    lbOut.Text = lbOut.Text & mlst.Get(i) & CRLF
  Next
End Sub

I've also attached a zip file that contains the Eclipse workspace project (I hope this can be imported into your eclipse workspace, but I am sure the Build paths will need to be modified), the compiled library.jar and.XML files, and the B4A project export.

I hope this might be a useful starting point for someone.

Note: I just realized the B4A code assumes landscape orientation but I did not explicitly set the orientation to landscape. If the phone is held in portrait mode some of the buttons may not be visible.

Barry.
 

Attachments

  • CRTlr.zip
    28.8 KB · Views: 228
Last edited:
Top