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

Commit de513b6

Browse files
更新初始化方式,初始化时默认关闭GPU加速防止部分手机奔溃
1 parent 0123fd9 commit de513b6

File tree

14 files changed

+318
-367
lines changed

14 files changed

+318
-367
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ android {
1010
applicationId "com.example.open_nsfw_android"
1111
minSdkVersion 15
1212
targetSdkVersion 28
13-
versionCode 4
14-
versionName "1.2.7"
13+
versionCode 5
14+
versionName "1.2.8"
1515
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1616
}
1717
buildTypes {
@@ -38,6 +38,6 @@ dependencies {
3838

3939
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
4040
implementation 'com.github.LuckSiege.PictureSelector:picture_library:v2.2.3'
41-
// implementation project(path: ':nsfw')
42-
implementation 'com.github.devzwy:open_nsfw_android:1.2.7'
41+
implementation project(path: ':nsfw')
42+
// implementation 'com.github.devzwy:open_nsfw_android:1.2.7'
4343
}

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

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.example.open_nsfw_android
22

33
import android.Manifest
44
import android.annotation.SuppressLint
5+
import android.app.ProgressDialog
56
import android.content.Intent
67
import android.content.pm.PackageManager
78
import android.graphics.BitmapFactory
@@ -15,18 +16,21 @@ import com.luck.picture.lib.PictureSelector
1516
import com.luck.picture.lib.config.PictureConfig
1617
import com.luck.picture.lib.config.PictureMimeType
1718
import com.luck.picture.lib.entity.LocalMedia
18-
import com.zwy.nsfw.api.NsfwHelper
19+
import com.zwy.nsfw.api.NSFWHelper
20+
import com.zwy.nsfw.core.NSFWConfig
1921
import kotlinx.android.synthetic.main.activity_main.*
22+
import kotlin.concurrent.thread
2023

2124

2225
class MainActivity : AppCompatActivity(), View.OnClickListener {
2326

24-
var nsfwHelper: NsfwHelper? = null
27+
var nsfwHelper: NSFWHelper? = null
2528
var mainAdapter: MainAdapter? = null
2629
var index = 0
27-
var listData: ArrayList<MyNsfwBean> = ArrayList<MyNsfwBean>()
30+
var listData: ArrayList<MyNsfwBean> = ArrayList()
2831
var selectList: List<LocalMedia>? = null
2932

33+
var progressDialog: ProgressDialog? = null
3034
@SuppressLint("SetTextI18n")
3135
override fun onCreate(savedInstanceState: Bundle?) {
3236
super.onCreate(savedInstanceState)
@@ -35,10 +39,13 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
3539
initAdapter()
3640
initClickListener()
3741
tv_version.text = "当前版本:${this.packageManager.getPackageInfo(packageName, 0).versionName}"
38-
if (ContextCompat.checkSelfPermission(this,
39-
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ //表示未授权时
42+
if (ContextCompat.checkSelfPermission(
43+
this,
44+
Manifest.permission.WRITE_EXTERNAL_STORAGE
45+
) != PackageManager.PERMISSION_GRANTED
46+
) { //表示未授权时
4047
//进行授权
41-
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),1);
48+
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1);
4249
}
4350
}
4451

@@ -88,46 +95,56 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
8895
}
8996

9097
private fun initNsfwHelper() {
91-
nsfwHelper = NsfwHelper.getInstance(this, true, 4)
98+
nsfwHelper = NSFWHelper.init(NSFWConfig(assets))
9299
}
93100

94101
private fun reScFromImgs(list: List<LocalMedia>) {
102+
progressDialog = ProgressDialog.show(this, "提示", "请稍后")
95103
index = 0
96104
mainAdapter?.setNewData(null)
97105
listData = ArrayList<MyNsfwBean>()
98106
Thread(Runnable {
99107
for (lm in list) {
100108
val bitmap = BitmapFactory.decodeFile(lm.path)
101109
listData.add(MyNsfwBean(0.0f, 0.0f, lm.path, bitmap))
102-
nsfwHelper?.scanBitmap(bitmap) { sfw, nsfw ->
103-
listData[index].sfw = sfw
104-
listData[index].nsfw = nsfw
105-
mainAdapter?.addData(listData[index])
106-
mainAdapter?.notifyItemInserted(index)
107-
rv.scrollToPosition(index)
108-
index++
109-
}
110+
val nsfwBean = nsfwHelper?.scanBitmap(bitmap)!!
111+
listData[index].sfw = nsfwBean.sfw
112+
listData[index].nsfw = nsfwBean.nsfw
113+
// rv.scrollToPosition(index)
114+
index++
115+
}
116+
runOnUiThread {
117+
mainAdapter?.setNewData(listData)
118+
mainAdapter?.notifyDataSetChanged()
119+
progressDialog?.dismiss()
110120
}
111121
}).start()
112122
}
113123

114124
private fun reScAssetsImgs() {
125+
progressDialog = ProgressDialog.show(this, "提示", "请稍后")
115126
index = 0
116127
mainAdapter?.setNewData(null)
117128
listData = ArrayList<MyNsfwBean>()
118-
for (a in resources.assets.list("img")) {
119-
val path = "img/${a}"
120-
val b = BitmapFactory.decodeStream(resources.assets.open(path))
121-
listData.add(MyNsfwBean(0f, 0f, path, b))
122-
nsfwHelper?.scanBitmap(b) { sfw, nsfw ->
123-
listData[index].sfw = sfw
124-
listData[index].nsfw = nsfw
125-
mainAdapter?.addData(listData[index])
126-
mainAdapter?.notifyItemInserted(index)
127-
rv.scrollToPosition(index)
129+
thread(true) {
130+
for (a in resources.assets.list("img")) {
131+
val path = "img/${a}"
132+
val b = BitmapFactory.decodeStream(resources.assets.open(path))
133+
listData.add(MyNsfwBean(0f, 0f, path, b))
134+
val nsfwBean = nsfwHelper?.scanBitmap(b)!!
135+
listData[index].sfw = nsfwBean.sfw
136+
listData[index].nsfw = nsfwBean.nsfw
137+
128138
index++
129139
}
140+
runOnUiThread {
141+
mainAdapter?.setNewData(listData)
142+
mainAdapter?.notifyDataSetChanged()
143+
progressDialog?.dismiss()
144+
// rv.scrollToPosition(index)
145+
}
130146
}
147+
131148
}
132149

133150
override fun onBackPressed() {
@@ -136,6 +153,6 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
136153

137154
override fun onDestroy() {
138155
super.onDestroy()
139-
nsfwHelper?.destroy()
156+
nsfwHelper?.destroyFactory()
140157
}
141158
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.example.open_nsfw_android
22

33
import android.graphics.Bitmap
4-
import com.zwy.nsfw.api.NsfwBean
54

6-
data class MyNsfwBean(var sfw: Float,var nsfw: Float, val path: String,val bitmap:Bitmap) {
7-
8-
}
5+
data class MyNsfwBean(var sfw: Float, var nsfw: Float, val path: String, val bitmap: Bitmap)

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.3.31'
4+
ext.kotlin_version = '1.3.41'
55
repositories {
66
google()
77
jcenter()
88

99
}
1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.4.1'
11+
classpath 'com.android.tools.build:gradle:3.4.2'
1212
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1313
// NOTE: Do not place your application dependencies here; they belong
1414
// in the individual module build.gradle files

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ org.gradle.jvmargs=-Xmx1536m
1313
# org.gradle.parallel=true
1414
# Kotlin code style for this project: "official" or "obsolete":
1515
kotlin.code.style=official
16+
kotlin.coroutines=enable

nsfw/build.gradle

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
24

35
android {
46
compileSdkVersion 28
57

68
defaultConfig {
79
minSdkVersion 15
810
targetSdkVersion 28
9-
versionCode 1
10-
versionName "1.0"
11+
versionCode 5
12+
versionName "1.2.8"
1113
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1214
}
1315

1416
buildTypes {
15-
debug{
17+
debug {
1618
minifyEnabled false //开启混淆®
1719
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
1820
}
1921
}
22+
kotlin {
23+
experimental {
24+
coroutines 'enable'
25+
}
26+
}
2027
}
2128

2229
dependencies {
@@ -27,4 +34,8 @@ dependencies {
2734
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
2835
implementation 'org.tensorflow:tensorflow-lite:+'
2936
implementation 'org.tensorflow:tensorflow-lite-gpu:+'
37+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
38+
}
39+
repositories {
40+
mavenCentral()
3041
}

0 commit comments

Comments
 (0)