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

Commit 410ae04

Browse files
committed
优化demo,修复异步扫描时多线程同事操作一块数据时引发的异常。
1 parent ecd4338 commit 410ae04

File tree

9 files changed

+156
-76
lines changed

9 files changed

+156
-76
lines changed

app/build.gradle

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,24 @@ android {
2727
aaptOptions {
2828
noCompress "tflite"
2929
}
30-
sourceSets {
31-
main {
32-
jniLibs.srcDirs = ['libs']
33-
assets.srcDir("main/assets")
34-
}
35-
}
30+
// sourceSets {
31+
// main {
32+
// jniLibs.srcDirs = ['libs']
33+
// assets.srcDir("main/assets")
34+
// }
35+
// }
3636
}
3737

3838
dependencies {
3939
implementation fileTree(include: ['*.jar'], dir: 'libs')
4040
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
4141
implementation 'com.android.support:appcompat-v7:28.0.0'
4242
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
43+
implementation 'com.android.support:design:28.0.0'
4344
testImplementation 'junit:junit:4.12'
4445
androidTestImplementation 'com.android.support.test:runner:1.0.2'
4546
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
46-
// implementation project(path: ':nsfw')
47-
implementation 'com.github.devzwy:open_nsfw_android:1.2.2'
47+
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
48+
implementation project(path: ':nsfw')
49+
// implementation 'com.github.devzwy:open_nsfw_android:1.2.3'
4850
}

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

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import android.annotation.SuppressLint
44
import android.graphics.BitmapFactory
55
import android.os.Bundle
66
import android.support.v7.app.AppCompatActivity
7-
import android.util.Log
7+
import android.support.v7.widget.LinearLayoutManager
88
import com.zwy.nsfw.api.NsfwHelper
99
import kotlinx.android.synthetic.main.activity_main.*
1010

1111

1212
class MainActivity : AppCompatActivity() {
1313
var nsfwHelper: NsfwHelper? = null
14-
15-
14+
var mainAdapter: MainAdapter? = null
15+
var index = 0
16+
val listData: ArrayList<MyNsfwBean> = ArrayList<MyNsfwBean>()
1617
@SuppressLint("SetTextI18n")
1718
override fun onCreate(savedInstanceState: Bundle?) {
1819
super.onCreate(savedInstanceState)
@@ -21,51 +22,24 @@ class MainActivity : AppCompatActivity() {
2122
// val b = BitmapFactory.decodeStream(resources.assets.open("img/06 (1).jpg"))
2223
// iv.setImageBitmap(b)
2324
nsfwHelper = NsfwHelper.getInstance(this, true, 1)
24-
25-
bt_.setOnClickListener {
26-
//同步识别
25+
mainAdapter = MainAdapter(null)
26+
rv.layoutManager = LinearLayoutManager(this)
27+
rv.adapter = mainAdapter
28+
tv_start.setOnClickListener {
2729
for (a in resources.assets.list("img")) {
28-
val b = BitmapFactory.decodeStream(resources.assets.open("img/${a}"))
29-
val nsfwBean = nsfwHelper?.scanBitmapSyn(b)
30-
Log.d("demo", nsfwBean.toString() + " - ${a}")
31-
tvv.text = "识别成功:\n\tSFW score : ${nsfwBean?.sfw}\n\tNSFW score : ${nsfwBean?.nsfw},- ${a}"
32-
if (nsfwBean?.nsfw ?: 0f > 0.7) {
33-
tvv.text = "${tvv.text} \n \t - 色情图片"
34-
} else {
35-
tvv.text = "${tvv.text} \n \t - 正常图片"
30+
val path = "img/${a}"
31+
val b = BitmapFactory.decodeStream(resources.assets.open(path))
32+
listData.add(MyNsfwBean(0f, 0f, path, b))
33+
nsfwHelper?.scanBitmap(b) { sfw, nsfw ->
34+
listData[index].sfw = sfw
35+
listData[index].nsfw = nsfw
36+
mainAdapter?.addData(listData[index])
37+
mainAdapter?.notifyItemInserted(index)
38+
index++
3639
}
3740
}
38-
// val nsfwBean = nsfwHelper?.scanBitmapSyn(b)
39-
// Log.d("demo", nsfwBean.toString())
40-
// tvv.text = "识别成功:\n\tSFW score : ${nsfwBean?.sfw}\n\tNSFW score : ${nsfwBean?.nsfw}"
41-
// if (nsfwBean?.nsfw ?: 0f > 0.7) {
42-
// tvv.text = "${tvv.text} \n \t - 色情图片"
43-
// } else {
44-
// tvv.text = "${tvv.text} \n \t - 正常图片"
45-
// }
46-
// //异步识别,接口回调识别结果
47-
// nsfwHelper?.scanBitmap(b) { sfw, nsfw ->
48-
// Log.d("demo", "sfw:$sfw,nsfw:$nsfw")
49-
// }
50-
}
51-
}
5241

53-
// override fun onResume() {
54-
// super.onResume()
55-
// if (!OpenCVLoader.initDebug()) {
56-
// OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
57-
// } else {
58-
// mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
59-
// }
60-
// }
42+
}
6143

62-
// internal var mLoaderCallback: LoaderCallbackInterface = object : LoaderCallbackInterface {
63-
// override fun onManagerConnected(status: Int) {
64-
//
65-
// }
66-
//
67-
// override fun onPackageInstall(operation: Int, callback: InstallCallbackInterface) {
68-
//
69-
// }
70-
// }
44+
}
7145
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.example.open_nsfw_android
2+
3+
import android.annotation.SuppressLint
4+
import android.support.v4.content.ContextCompat
5+
import android.widget.ImageView
6+
import android.widget.RelativeLayout
7+
import android.widget.TextView
8+
import com.chad.library.adapter.base.BaseQuickAdapter
9+
import com.chad.library.adapter.base.BaseViewHolder
10+
11+
class MainAdapter(val nsfwList: List<MyNsfwBean>?) :
12+
BaseQuickAdapter<MyNsfwBean, BaseViewHolder>(R.layout.main_item, nsfwList) {
13+
14+
@SuppressLint("SetTextI18n")
15+
override fun convert(helper: BaseViewHolder, item: MyNsfwBean) {
16+
val textView = helper.getView<TextView>(R.id.tv_text)
17+
val imageView = helper.getView<ImageView>(R.id.iv)
18+
val view = helper.getView<RelativeLayout>(R.id.view)
19+
var nsfwStr = "色情图片"
20+
var color = ContextCompat.getColor(mContext,R.color.nsfw1)
21+
when (item.nsfw) {
22+
in 0.0..0.3 -> {
23+
nsfwStr = "正常图片"
24+
color = ContextCompat.getColor(mContext,R.color.nsfw3)
25+
}
26+
in 0.3..0.7 -> {
27+
nsfwStr = "👙比基尼"
28+
color = ContextCompat.getColor(mContext,R.color.nsfw2)
29+
}
30+
}
31+
textView.text =
32+
"path = ${"img/${item.path}"} \n\nSFW score: ${item.sfw}\nNSFW score: ${item.nsfw} \n\n 鉴定结果: ${nsfwStr}"
33+
imageView.setImageBitmap(item.bitmap)
34+
view.setBackgroundColor(color)
35+
}
36+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.open_nsfw_android
2+
3+
import android.graphics.Bitmap
4+
import com.zwy.nsfw.api.NsfwBean
5+
6+
data class MyNsfwBean(var sfw: Float,var nsfw: Float, val path: String,val bitmap:Bitmap) {
7+
8+
}
Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
4-
android:gravity="center"
5-
android:layout_width="match_parent"
6-
android:layout_height="match_parent">
7-
<ImageView android:layout_width="match_parent"
8-
9-
android:id="@+id/iv" android:layout_height="300dp"/>
10-
<Button android:layout_width="wrap_content"
11-
android:layout_below="@+id/iv"
12-
android:layout_centerHorizontal="true"
13-
android:text="点击识别图片"
14-
android:id="@+id/bt_"
15-
android:gravity="center"
16-
android:layout_height="wrap_content"/>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:orientation="vertical"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
177
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
18-
android:layout_below="@+id/bt_"
19-
android:id="@+id/tv_"
8+
android:textSize="18sp"
9+
android:id="@+id/tv_start"
10+
android:gravity="center"
11+
android:layout_gravity="center"
2012
android:padding="20dp"
21-
android:text="sfw:适宜在公共场所浏览,nsfw:不适宜在公共场所浏览,两者都是0-1中间的浮点型值,nsfw数值越大表示色情程度越高,sfw反之"/>
22-
<TextView
23-
android:id="@+id/tvv"
24-
android:layout_below="@+id/tv_" android:layout_width="match_parent" android:layout_height="match_parent"/>
13+
android:text="点击开始识别Assets下的测试图片"/>
14+
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
15+
android:text="sfw:适宜在公共场所浏览,nsfw:不适宜在公共场所浏览,两者都是0-1中间的浮点型值,nsfw数值越大表示色情程度越高,sfw反之"
16+
android:padding="15dp"
17+
android:textSize="14sp"/>
18+
<android.support.v7.widget.RecyclerView
19+
android:id="@+id/rv"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"/>
22+
<!--<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">-->
23+
<!--<LinearLayout android:layout_width="match_parent"-->
24+
<!--android:orientation="vertical" android:layout_height="match_parent">-->
25+
<!--<Button android:layout_width="wrap_content"-->
26+
<!--android:layout_centerHorizontal="true"-->
27+
<!--android:text="点击开始识别Assets下的测试图片"-->
28+
<!--android:layout_gravity="center"-->
29+
<!--android:id="@+id/bt_"-->
30+
<!--android:gravity="center"-->
31+
<!--android:layout_height="wrap_content"/>-->
32+
<!--<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"-->
33+
<!--android:layout_below="@+id/bt_"-->
34+
<!--android:id="@+id/tv_"-->
35+
<!--android:padding="20dp"-->
36+
<!--android:text="sfw:适宜在公共场所浏览,nsfw:不适宜在公共场所浏览,两者都是0-1中间的浮点型值,nsfw数值越大表示色情程度越高,sfw反之"/>-->
37+
<!--<TextView-->
38+
<!--android:id="@+id/tvv"-->
39+
<!--android:layout_width="match_parent"-->
40+
<!--android:padding="20dp"-->
41+
<!--android:textColor="@color/colorAccent"-->
42+
<!--android:textSize="16sp"-->
43+
<!--android:layout_height="match_parent"/>-->
44+
45+
<!--</LinearLayout>-->
46+
<!--</ScrollView>-->
2547

2648

27-
</RelativeLayout>
49+
</LinearLayout>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:id="@+id/view"
5+
android:layout_marginTop="5dp"
6+
android:padding="20dp"
7+
8+
android:layout_height="wrap_content">
9+
<TextView android:layout_width="match_parent"
10+
android:id="@+id/tv_text"
11+
android:layout_toLeftOf="@+id/iv"
12+
android:layout_height="wrap_content"/>
13+
<ImageView android:layout_width="100dp"
14+
android:layout_height="100dp"
15+
android:id="@+id/iv"
16+
android:scaleType="centerCrop"
17+
android:layout_alignParentRight="true"/>
18+
</RelativeLayout>

app/src/main/res/values/colors.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
<color name="colorPrimary">#008577</color>
44
<color name="colorPrimaryDark">#00574B</color>
55
<color name="semi_transparent">#66000000</color>
6+
<color name="nsfw1">#20EE1E0D</color>
7+
<color name="nsfw2">#20FD9904</color>
8+
<color name="nsfw3">#FFFFFF</color>
69
<color name="colorAccent">#D81B60</color>
10+
711
</resources>

nsfw/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native
6363
}
6464
tasks.withType(JavaCompile) {
6565
compileTask -> compileTask.dependsOn(nativeLibsToJar)
66+
// implementation project(path: ':openCVLibrary340')
6667
}

nsfw/src/main/java/com/zwy/nsfw/api/NsfwHelper.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,26 @@ private NsfwHelper(Activity activity, Boolean isAddGpuDelegate, int numThreads)
3333
}
3434
}
3535

36+
37+
/**
38+
* 同步扫描
39+
*
40+
* @param bitmap
41+
* @return nsfw/sfw
42+
*/
3643
public NsfwBean scanBitmapSyn(Bitmap bitmap) {
37-
return classifier.run(bitmap);
44+
synchronized (NsfwHelper.class) {
45+
return classifier.run(bitmap);
46+
}
3847
}
3948

4049

50+
/**
51+
* 异步扫描
52+
*
53+
* @param bitmap
54+
* @param onScanBitmapListener void onSuccess(float sfw, float nsfw);
55+
*/
4156
public void scanBitmap(final Bitmap bitmap, final OnScanBitmapListener onScanBitmapListener) {
4257
new Thread(new Runnable() {
4358
@Override

0 commit comments

Comments
 (0)