0

I am currently developing an app in Android Studio [using Java], and I have the challenge of preventing users from uploading nude pictures into the database. Luckily, I found this library on GitHub called NSFW(Nude Content) Detector Android.

My challenge is that it was written in Kotlin and the usage example given is also in Kotlin. I know Java and Kotlin are interoperable but I just can't figure it out after several tries.

The usage given is:

NSFWDetector.isNSFW(bitmap,confidenceThreshold) { isNSFW, confidence, image ->
    if (isNSFW){
        Toast.makeText(this, "NSFW with confidence: $confidence", Toast.LENGTH_SHORT).show()
    } else {
        Toast.makeText(this, "SFW with confidence: $confidence", Toast.LENGTH_SHORT).show()
    }
}

I know that bitmap is the image I am testing, and confidenceThreshold is the float number of the level of testing, but I can't figure the rest out. I saw these answers 1, 2, and 3 on Stack Overflow but they don't have these kind of callbacks.

Please, how do I implement this in Java?

Thank you for the help.

oracle
  • 41
  • 8

1 Answers1

4

Make sure you're using Java 8 so you have lambda support:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

In Kotlin, $ in a string literal allows you to put a variable value directly in the String. In this case "NSFW with confidence: $confidence" in Kotlin would be "NSFW with confidence: " + confidence in Java. So change both of your Strings that way.

Kotlin lambdas have braces around them and the whole lambda can be placed outside the parentheses of the method you're calling. So remove the braces and move it inside the method call parentheses. Also, Java require parentheses around the lambda parameters if there are more than one. And it will require braces around the lambda statement since it isn't a single expression.

Finally, since the library is defined in Kotlin, you have to explicitly return Unit rather than implicitly returning void like you would in a Java method.

So your Kotlin code in Java looks like:

NSFWDetector.INSTANCE.isNSFW(bitmap,confidenceThreshold, (isNSFW, confidence, image) -> {
    if (isNSFW){
        Toast.makeText(this, "NSFW with confidence: " + confidence, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "SFW with confidence: " + confidence, Toast.LENGTH_SHORT).show();
    }
    return kotlin.Unit.INSTANCE;
});
oracle
  • 41
  • 8
Tenfour04
  • 39,254
  • 8
  • 47
  • 96
  • Thank you for your reply. I have tried it and the previous errors are gone, but it does have an error in the last curly brace that "Missing return statement". Any ideas please? – oracle Oct 10 '20 at 22:40
  • @oracle Sorry, I didn't realize the library was written in Kotlin (not just the example). See the edited code above. – Tenfour04 Oct 10 '20 at 23:33
  • Oh my gosh, it worked like magic. You have helped me a lot. I really appreciate @Tenfour04 :) – oracle Oct 11 '20 at 02:44