Android Question HOW ADD THIS API TO PROJECT

drgottjr

Expert
Licensed User
Longtime User
you'll want okhttputils2 postmultipart https://www.b4x.com/android/forum/t...with-json-and-image-files.114123/#post-716027
to show you how to upload an image to a server.

you need to read remove.bg's instructions very, very carefully. success depends on your needs and how you follow their instructions. the b4a part is probably the simplest part of the operation. have you ever used okhttputils2 to upload to/download from the internet? you should practice first with it.

the example on remove.bg's page is a unix utility called "curl". the
"-H" option is a header. okhttputils2 handles adding headers to your request. do you know what an HTTP header is? adding headers
"-F" option is the name of the image file. this is where you will need to study the link above. and remove.bg's instructions.
"-f" option is going to be a problem, i believe. remove.bg is not using the "-f" option in accordance with curl's documentation. but the operation might work without using it. what happens is remove.bg creates a file called "no-bg.png", which is your file minus the background. that's what it returns to you. after you upload the file, you wait for remove.bg to send the file back to you. remove.bg calls it "no-bg.png", but you don't have to call it that. what you receive is a stream of bytes. you read that stream into a file of your own naming. do you follow this?

practice a little with okhttputils2. it's simple and reliable. but if you send something remove.bg isn't expecting, you will get an error. then you come back here and report the complete error. computers are very strict regarding what they expect from an exterior source.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
in the end, it should look SOMETHING like this: (make sure you include okhttp and okhttputils from the add'l libraries panel)

B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "file"
    fd.Dir = File.DirInternal
    fd.FileName = "file.jpg"
    fd.ContentType = "image/jpg"
    j.PostMultipart("https://api.remove.bg/v1.0/removebg -o no-bg.png", Null, fd)
    j.GetRequest.SetHeader("X-API-Key", "YOUR_API_KEY")

    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim bitmap As Bitmap = j.GetBitmap
    else
       log("Uh oh!: " & lastexception.message)
    End If
    j.Release

there are a couple other ways to handle it, but i don't have an api key (don't want one, thanks), so i can't try it. but this is the general setup.
as you get errors, you report them (NOT: "i got an error. help." we need to see exactly what the server says):oops:
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
fd.KeyName = "file"
If you look at the specs (poster linked to it), this should be
B4X:
fd.KeyName = "image_file"
Also,
j.PostMultipart("https://api.remove.bg/v1.0/removebg -o no-bg.png", Null, fd)
needs to be
B4X:
j.PostMultipart("https://api.remove.bg/v1.0/removebg", Null, fd)
The
-o no-bg.png
needs to be handled in
If j.Success Then Dim bitmap As Bitmap = j.GetBitmap else
portion of the code. Hint: https://www.b4x.com/android/forum/threads/write-bitmap-to-file.52280/post-327568
To repeat what @drgottjr said, post any issues here, with full information on error messages received from B4A, the server, etc. Also, post your code here that you use to achieve your goal (sans API key, don't want to make that public). Feel free to like @drgottjr's posts for the help they provide (he's been helping a lot of people! Thank you!).
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
@OliverA: there's something about:
B4X:
j.PostMultipart("https://api.remove.bg/v1.0/removebg -o no-bg.png", Null, fd)

i think "-o no-bg.png" is basically a placeholder to make the server happy. otherwise it wouldn't know what to name the return "file". but the name is irrelevant. i'm thinking the user could just as easily put "-o myfile.png" and get the same result.

i don't believe there is anything about the file name to handle in the download since the download is a stream of bytes, not an actual file. i don't think the name no-bg.png or myfile.png will appear in the download (except maybe in the return headers, which the user rarely considers). the user could save the bitmap as "no-bg.png" or whatever he wanted. it's like when you ask to download an html file; you use j.getString, and you get your string of bytes. how do you see it?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The -o is a curl option, not a server option. The filename is given by curl. Server just sends back the raw data (in this case an image with the background removed). I'm not an authority on the given API, I'm just looking at the curl options.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Matteo Granatiero

Active Member
Licensed User
in the end, it should look SOMETHING like this: (make sure you include okhttp and okhttputils from the add'l libraries panel)

B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "file"
    fd.Dir = File.DirInternal
    fd.FileName = "file.jpg"
    fd.ContentType = "image/jpg"
    j.PostMultipart("https://api.remove.bg/v1.0/removebg -o no-bg.png", Null, fd)
    j.GetRequest.SetHeader("X-API-Key", "YOUR_API_KEY")

    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim bitmap As Bitmap = j.GetBitmap
    else
       log("Uh oh!: " & lastexception.message)
    End If
    j.Release

there are a couple other ways to handle it, but i don't have an api key (don't want one, thanks), so i can't try it. but this is the general setup.
as you get errors, you report them (NOT: "i got an error. help." we need to see exactly what the server says):oops:

it works great, it takes 4-5 seconds but it is absolutely worth it. THANKS TO ALL FOR YOUR TIME AND COMMITMENT
 

Attachments

  • img.png
    img.png
    24.3 KB · Views: 111
Upvote 0
Top