-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
354 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/src/main/java/com/hzy/dlib/simple/app/activity/DetectFileActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.hzy.dlib.simple.app.activity; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.Rect; | ||
import android.os.Bundle; | ||
import android.view.MenuItem; | ||
import android.widget.ImageView; | ||
|
||
import androidx.annotation.Nullable; | ||
import androidx.appcompat.app.ActionBar; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import com.alibaba.android.arouter.facade.annotation.Route; | ||
import com.blankj.utilcode.util.FileIOUtils; | ||
import com.blankj.utilcode.util.ImageUtils; | ||
import com.blankj.utilcode.util.SnackbarUtils; | ||
import com.hzy.dlib.libdlib.DLibDetector; | ||
import com.hzy.dlib.simple.app.R; | ||
import com.hzy.dlib.simple.app.consts.RouterHub; | ||
import com.hzy.dlib.simple.app.utils.BitmapDrawUtils; | ||
import com.hzy.dlib.simple.app.utils.DetectUtils; | ||
import com.hzy.dlib.simple.app.utils.SpaceUtils; | ||
|
||
import java.io.File; | ||
|
||
import butterknife.BindView; | ||
import butterknife.ButterKnife; | ||
|
||
@Route(path = RouterHub.DETECT_FILE_ACTIVITY) | ||
public class DetectFileActivity extends AppCompatActivity { | ||
|
||
@BindView(R.id.demo_image) | ||
ImageView mDemoImage; | ||
private File mDetectFile; | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_detect_file); | ||
ButterKnife.bind(this); | ||
ActionBar actionBar = getSupportActionBar(); | ||
if (actionBar != null) { | ||
actionBar.setDisplayHomeAsUpEnabled(true); | ||
} | ||
prepareAndDetectFile(); | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(MenuItem item) { | ||
switch (item.getItemId()) { | ||
case android.R.id.home: | ||
finish(); | ||
return true; | ||
} | ||
return super.onOptionsItemSelected(item); | ||
} | ||
|
||
private void snakeBarShow(String msg) { | ||
SnackbarUtils.with(mDemoImage).setMessage(msg).show(); | ||
} | ||
|
||
private void prepareAndDetectFile() { | ||
new Thread() { | ||
@Override | ||
public void run() { | ||
try { | ||
File dir = SpaceUtils.getUsableFilePath(); | ||
mDetectFile = new File(dir, DetectUtils.DEMO_ASSET_NAME); | ||
if (!mDetectFile.exists()) { | ||
FileIOUtils.writeFileFromIS(mDetectFile, | ||
getAssets().open(DetectUtils.DEMO_ASSET_NAME)); | ||
} | ||
loadAndShowImage(null); | ||
Rect[] faces = DLibDetector.INSTANCE.detectFromFile(mDetectFile.getPath()); | ||
loadAndShowImage(faces); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}.start(); | ||
} | ||
|
||
private void loadAndShowImage(Rect[] faces) { | ||
Bitmap bitmap = ImageUtils.getBitmap(mDetectFile); | ||
Bitmap newBitmap = bitmap.copy(bitmap.getConfig(), true); | ||
bitmap.recycle(); | ||
if (faces != null) { | ||
BitmapDrawUtils.drawRectOnBitmap(newBitmap, faces); | ||
} | ||
mDemoImage.post(() -> { | ||
mDemoImage.setImageBitmap(newBitmap); | ||
if (faces != null) { | ||
snakeBarShow(faces.length + " Faces Detected!!"); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
app/src/main/java/com/hzy/dlib/simple/app/consts/RouterHub.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.hzy.dlib.simple.app.consts; | ||
|
||
public class RouterHub { | ||
public static final String DETECT_ACTIVITY = "/main/DetectActivity"; | ||
public static final String DETECT_BITMAP_ACTIVITY = "/main/DetectBitmapActivity"; | ||
public static final String DETECT_FILE_ACTIVITY = "/main/DetectFileActivity"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/src/main/java/com/hzy/dlib/simple/app/utils/SpaceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.hzy.dlib.simple.app.utils; | ||
|
||
import android.os.Environment; | ||
|
||
import com.blankj.utilcode.util.FileUtils; | ||
import com.blankj.utilcode.util.Utils; | ||
|
||
import java.io.File; | ||
import java.util.UUID; | ||
|
||
public class SpaceUtils { | ||
|
||
public static final String WORKSPACE_DIR = "workspace"; | ||
|
||
/** | ||
* new some file with random file name | ||
* | ||
* @return file object | ||
*/ | ||
public static File newUsableFile() { | ||
File fileDir = getUsableFilePath(); | ||
if (fileDir.exists()) { | ||
String randomName = UUID.randomUUID().toString(); | ||
return new File(fileDir, randomName); | ||
} | ||
return null; | ||
} | ||
|
||
public static void clearUsableSpace() { | ||
File spacePath = getUsableFilePath(); | ||
if (spacePath.exists()) { | ||
FileUtils.deleteAllInDir(spacePath); | ||
} | ||
} | ||
|
||
/** | ||
* get some available path to store files | ||
* | ||
* @return some path | ||
*/ | ||
public static File getUsableFilePath() { | ||
File fileDir = null; | ||
try { | ||
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { | ||
fileDir = Utils.getApp().getExternalFilesDir(WORKSPACE_DIR); | ||
} | ||
if (fileDir == null) { | ||
fileDir = new File(Utils.getApp().getFilesDir(), WORKSPACE_DIR); | ||
} | ||
if (!fileDir.exists()) { | ||
fileDir.mkdirs(); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return fileDir; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<corners android:radius="6dp" /> | ||
<stroke | ||
android:width="1px" | ||
android:color="@color/colorAccent" /> | ||
</shape> |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
android:padding="6dp"> | ||
|
||
<ImageView | ||
android:id="@+id/demo_image" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="#f7f7f7" | ||
android:maxHeight="400dp" /> | ||
|
||
</LinearLayout> |
Oops, something went wrong.