How do I use this [NSFW(Nude Content) Detector Android] Kotlin library in Java?

huangapple go评论81阅读模式
英文:

How do I use this [NSFW(Nude Content) Detector Android] Kotlin library in Java?

问题

以下是翻译好的内容:

我目前正在使用Android Studio(使用Java)开发应用程序,我面临的挑战是防止用户将裸露图片上传到数据库中。幸运的是,我在GitHub上找到了一个名为NSFW(裸体内容)检测器Android的库。

我的挑战在于,它是用Kotlin编写的,给出的用法示例也是用Kotlin编写的。我知道Java和Kotlin是可互操作的,但经过几次尝试后,我仍然无法弄清楚。

给出的用法如下:

NSFWDetector.isNSFW(bitmap, confidenceThreshold, new NSFWDetectionCallback() {
    @Override
    public void onNSFWDetected(boolean isNSFW, float confidence, Bitmap image) {
        if (isNSFW) {
            Toast.makeText(MainActivity.this, "NSFW with confidence: " + confidence, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "SFW with confidence: " + confidence, Toast.LENGTH_SHORT).show();
        }
    }
});

我知道bitmap是我要测试的图像,而confidenceThreshold是测试级别的浮点数,但我无法弄清楚其余部分。我在Stack Overflow上看到了这些答案123,但它们没有这种类型的回调。

请问,我该如何在Java中实现这个?

谢谢你的帮助。

英文:

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.

答案1

得分: 5

确保您正在使用Java 8,以便您拥有lambda支持:
```lang-groovy
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

在Kotlin中,字符串字面量中的$允许您直接在字符串中放置变量值。在这种情况下,Kotlin中的"NSFW with confidence: $confidence"将变为Java中的"NSFW with confidence: " + confidence。因此,按此方式更改您的两个字符串。

Kotlin的lambda在其周围有大括号,并且整个lambda可以放在您调用的方法的括号之外。因此,删除大括号并将其移动到方法调用括号内。此外,如果lambda参数超过一个,在Java中需要在lambda参数周围加上括号。并且由于lambda语句不是单个表达式,因此它将需要在其周围加上大括号。

最后,由于库是用Kotlin定义的,在Java中,您必须显式返回Unit,而不是像在Java方法中隐式返回void那样。

因此,您的Kotlin代码在Java中如下所示:

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;
});
英文:

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;
});

huangapple
  • 本文由 发表于 2020年10月10日 22:48:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64294698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定