Skip to content
This repository was archived by the owner on Apr 8, 2023. It is now read-only.

Commit 48ab350

Browse files
更新Kotlin拓展函数支持,调用扫描更方便。
1 parent 8176f50 commit 48ab350

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ __请添加__
4040
```
4141

4242

43-
- Code like this
43+
- 使用
4444

4545
```
4646
val nsfwHelper = NSFWHelper.init(NSFWConfig(assets))
@@ -51,6 +51,32 @@ __请添加__
5151
Log.e("NSFW","图片涉黄")
5252
}
5353
```
54+
- kotlin可直接使用File.getNsfwScore(mAssetManager: AssetManager): NsfwBean 或 Bitmap.getNsfwScore(mAssetManager: AssetManager): NsfwBean 直接获取鉴定结果(NSFWHelper 1.2.9版本开始支持),比如:
55+
56+
```
57+
val bitmap = BitmapFactory.decodeFile(path)
58+
59+
val nsfwScore = bitmap.getNsfwScore(assets)
60+
61+
if(nsfwBean.nsfw>0.3){
62+
63+
Log.e("NSFW","图片涉黄")
64+
65+
}
66+
```
67+
68+
```
69+
val file = File(lm.path)
70+
71+
val nsfwScore = file.getNsfwScore(assets)
72+
73+
if(nsfwBean.nsfw>0.3){
74+
75+
Log.e("NSFW","图片涉黄")
76+
77+
}
78+
79+
```
5480

5581
### [点我下载apk](https://fir.im/nsfw)
5682

app/src/main/java/com/example/open_nsfw_android/MainActivity.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import com.luck.picture.lib.config.PictureMimeType
1818
import com.luck.picture.lib.entity.LocalMedia
1919
import com.zwy.nsfw.api.NSFWHelper
2020
import com.zwy.nsfw.core.NSFWConfig
21+
import com.zwy.nsfw.kotlin.getNsfwScore
2122
import kotlinx.android.synthetic.main.activity_main.*
23+
import java.io.File
2224
import kotlin.concurrent.thread
2325

2426

@@ -106,12 +108,18 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
106108
Thread(Runnable {
107109
for (lm in list) {
108110
val bitmap = BitmapFactory.decodeFile(lm.path)
109-
listData.add(MyNsfwBean(0.0f, 0.0f, lm.path, bitmap))
110-
val nsfwBean = nsfwHelper?.scanBitmap(bitmap)!!
111-
listData[index].sfw = nsfwBean.sfw
112-
listData[index].nsfw = nsfwBean.nsfw
111+
112+
val nsfwScore = bitmap.getNsfwScore(assets)
113+
114+
// listData.add(MyNsfwBean(0.0f, 0.0f, lm.path, bitmap))
115+
listData.add(MyNsfwBean(nsfwScore.sfw, nsfwScore.nsfw, lm.path, bitmap))
116+
// val nsfwBean = nsfwHelper?.scanBitmap(bitmap)!!
117+
// listData[index].sfw = nsfwBean.sfw
118+
// listData[index].nsfw = nsfwBean.nsfw
113119
// rv.scrollToPosition(index)
114120
index++
121+
122+
115123
}
116124
runOnUiThread {
117125
mainAdapter?.setNewData(listData)

nsfw/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
defaultConfig {
99
minSdkVersion 19
1010
targetSdkVersion 28
11-
versionCode 5
12-
versionName "1.2.8"
11+
versionCode 6
12+
versionName "1.2.9"
1313
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1414
}
1515

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.zwy.nsfw.kotlin
2+
3+
import android.content.res.AssetManager
4+
import android.graphics.Bitmap
5+
import android.graphics.BitmapFactory
6+
import com.zwy.nsfw.api.NSFWHelper
7+
import com.zwy.nsfw.core.NSFWConfig
8+
import com.zwy.nsfw.core.NsfwBean
9+
import java.io.File
10+
11+
12+
fun Bitmap.getNsfwScore(mAssetManager: AssetManager): NsfwBean {
13+
val nsfwBean = NSFWHelper.init(NSFWConfig(mAssetManager)).scanBitmap(this)
14+
NSFWHelper.destroyFactory()
15+
return nsfwBean
16+
}
17+
18+
fun File.getNsfwScore(mAssetManager: AssetManager): NsfwBean {
19+
val bitmap = try {
20+
BitmapFactory.decodeFile(this.path)
21+
} catch (e: Exception) {
22+
return NsfwBean(0f, 0f)
23+
}
24+
25+
val nsfwBean = NSFWHelper.init(NSFWConfig(mAssetManager)).scanBitmap(bitmap)
26+
NSFWHelper.destroyFactory()
27+
return nsfwBean
28+
}

0 commit comments

Comments
 (0)