flowerchecker / plant-id-examples

Example client's code for work with Plant.id identification API in various languages
MIT License
127 stars 44 forks source link

Create Sync Java Example #6

Closed ghost closed 3 years ago

ghost commented 4 years ago

Create Java Example

This should be a clean and easy to understand, almost entirely library-less java example for using this API. Related to issue #4 - 'Add example codes ...'

ghost commented 4 years ago

(contribution related to HacktoberFest. "hacktoberfest-accepted" label would be appreciated 😄)

Would also be nice :)

Krejdom commented 3 years ago

Sorry for the delay. I will try to merge by the end of the month. Could you please change it so it does not matter how many images there are as an input? Now there are exactly two variables for them and I would have to add more/or delete one. It would be great, if I could change this just in one place. I hope it is possible.

ghost commented 3 years ago

Sorry for the delay. I will try to merge by the end of the month. Could you please change it so it does not matter how many images there are as an input? Now there are exactly two variables for them and I would have to add more/or delete one. It would be great, if I could change this just in one place. I hope it is possible.

@Krejdom It's 2 lines of code to add an image.

String imageName = base64EncodeFromFile("filename");
images.put(imageName);

Its also very easy to set up a loop to do that. I an make this 1 line but that will make the example more complicated.

rlebre commented 3 years ago

@ransbachm I think that what @Krejdom is trying to say is that you can populate your JSONObject data dynamically depending on the list of files.

For instance, changing

// read image from local file system and encode
String flower1 = base64EncodeFromFile("flower1.jpg");
String flower2 = base64EncodeFromFile("flower2.jpg");

JSONObject data = new JSONObject();
data.put("api_key", apiKey);

// add images
JSONArray images = new JSONArray()
        .put(flower1)
        .put(flower2);
data.put("images", images);

to

String[] listOfFlowers = {"flower1.jpg", "flower2.jpg"};

JSONObject data = new JSONObject();
data.put("api_key", apiKey);

// add images
JSONArray images = new JSONArray();

for(String flower : flowersBase64Encoded)
        // read image from local file system and encode
        String flowerImageEncoded = base64EncodeFromFile(flower);
    images.put(flowerImageEncoded);

data.put("images", images);

This way, you can only change the number/name of the files in the first string array and not change the code in more lines. Hope it helps :)

ghost commented 3 years ago

Will do that

ghost commented 3 years ago

Done @Krejdom