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

Commit

Permalink
更新初始化方式,初始化时默认关闭GPU加速防止部分手机奔溃
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jul 26, 2019
1 parent 0123fd9 commit de513b6
Show file tree
Hide file tree
Showing 14 changed files with 318 additions and 367 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.example.open_nsfw_android"
minSdkVersion 15
targetSdkVersion 28
versionCode 4
versionName "1.2.7"
versionCode 5
versionName "1.2.8"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -38,6 +38,6 @@ dependencies {

implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
implementation 'com.github.LuckSiege.PictureSelector:picture_library:v2.2.3'
// implementation project(path: ':nsfw')
implementation 'com.github.devzwy:open_nsfw_android:1.2.7'
implementation project(path: ':nsfw')
// implementation 'com.github.devzwy:open_nsfw_android:1.2.7'
}
69 changes: 43 additions & 26 deletions app/src/main/java/com/example/open_nsfw_android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.open_nsfw_android

import android.Manifest
import android.annotation.SuppressLint
import android.app.ProgressDialog
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
Expand All @@ -15,18 +16,21 @@ import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.zwy.nsfw.api.NsfwHelper
import com.zwy.nsfw.api.NSFWHelper
import com.zwy.nsfw.core.NSFWConfig
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.concurrent.thread


class MainActivity : AppCompatActivity(), View.OnClickListener {

var nsfwHelper: NsfwHelper? = null
var nsfwHelper: NSFWHelper? = null
var mainAdapter: MainAdapter? = null
var index = 0
var listData: ArrayList<MyNsfwBean> = ArrayList<MyNsfwBean>()
var listData: ArrayList<MyNsfwBean> = ArrayList()
var selectList: List<LocalMedia>? = null

var progressDialog: ProgressDialog? = null
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -35,10 +39,13 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
initAdapter()
initClickListener()
tv_version.text = "当前版本:${this.packageManager.getPackageInfo(packageName, 0).versionName}"
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ //表示未授权时
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
) { //表示未授权时
//进行授权
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),1);
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1);
}
}

Expand Down Expand Up @@ -88,46 +95,56 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
}

private fun initNsfwHelper() {
nsfwHelper = NsfwHelper.getInstance(this, true, 4)
nsfwHelper = NSFWHelper.init(NSFWConfig(assets))
}

private fun reScFromImgs(list: List<LocalMedia>) {
progressDialog = ProgressDialog.show(this, "提示", "请稍后")
index = 0
mainAdapter?.setNewData(null)
listData = ArrayList<MyNsfwBean>()
Thread(Runnable {
for (lm in list) {
val bitmap = BitmapFactory.decodeFile(lm.path)
listData.add(MyNsfwBean(0.0f, 0.0f, lm.path, bitmap))
nsfwHelper?.scanBitmap(bitmap) { sfw, nsfw ->
listData[index].sfw = sfw
listData[index].nsfw = nsfw
mainAdapter?.addData(listData[index])
mainAdapter?.notifyItemInserted(index)
rv.scrollToPosition(index)
index++
}
val nsfwBean = nsfwHelper?.scanBitmap(bitmap)!!
listData[index].sfw = nsfwBean.sfw
listData[index].nsfw = nsfwBean.nsfw
// rv.scrollToPosition(index)
index++
}
runOnUiThread {
mainAdapter?.setNewData(listData)
mainAdapter?.notifyDataSetChanged()
progressDialog?.dismiss()
}
}).start()
}

private fun reScAssetsImgs() {
progressDialog = ProgressDialog.show(this, "提示", "请稍后")
index = 0
mainAdapter?.setNewData(null)
listData = ArrayList<MyNsfwBean>()
for (a in resources.assets.list("img")) {
val path = "img/${a}"
val b = BitmapFactory.decodeStream(resources.assets.open(path))
listData.add(MyNsfwBean(0f, 0f, path, b))
nsfwHelper?.scanBitmap(b) { sfw, nsfw ->
listData[index].sfw = sfw
listData[index].nsfw = nsfw
mainAdapter?.addData(listData[index])
mainAdapter?.notifyItemInserted(index)
rv.scrollToPosition(index)
thread(true) {
for (a in resources.assets.list("img")) {
val path = "img/${a}"
val b = BitmapFactory.decodeStream(resources.assets.open(path))
listData.add(MyNsfwBean(0f, 0f, path, b))
val nsfwBean = nsfwHelper?.scanBitmap(b)!!
listData[index].sfw = nsfwBean.sfw
listData[index].nsfw = nsfwBean.nsfw

index++
}
runOnUiThread {
mainAdapter?.setNewData(listData)
mainAdapter?.notifyDataSetChanged()
progressDialog?.dismiss()
// rv.scrollToPosition(index)
}
}

}

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

override fun onDestroy() {
super.onDestroy()
nsfwHelper?.destroy()
nsfwHelper?.destroyFactory()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.example.open_nsfw_android

import android.graphics.Bitmap
import com.zwy.nsfw.api.NsfwBean

data class MyNsfwBean(var sfw: Float,var nsfw: Float, val path: String,val bitmap:Bitmap) {

}
data class MyNsfwBean(var sfw: Float, var nsfw: Float, val path: String, val bitmap: Bitmap)
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.31'
ext.kotlin_version = '1.3.41'
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ org.gradle.jvmargs=-Xmx1536m
# org.gradle.parallel=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
kotlin.coroutines=enable
17 changes: 14 additions & 3 deletions nsfw/build.gradle
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
versionCode 5
versionName "1.2.8"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
debug{
debug {
minifyEnabled false //开启混淆®
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
kotlin {
experimental {
coroutines 'enable'
}
}
}

dependencies {
Expand All @@ -27,4 +34,8 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'org.tensorflow:tensorflow-lite:+'
implementation 'org.tensorflow:tensorflow-lite-gpu:+'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
Loading

0 comments on commit de513b6

Please sign in to comment.