Skip to content

Commit

Permalink
[Android] Add camera preview demo without the support of PaddleLite (P…
Browse files Browse the repository at this point in the history
  • Loading branch information
hong19860320 committed May 27, 2020
1 parent 3579465 commit 90b684a
Show file tree
Hide file tree
Showing 59 changed files with 2,621 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
.idea/
CMakeCache.txt
CMakeFiles/
Makefile
cmake_install.cmake
1 change: 1 addition & 0 deletions PaddleLite-android-demo/camera_preview_demo/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
182 changes: 182 additions & 0 deletions PaddleLite-android-demo/camera_preview_demo/app/app.iml

Large diffs are not rendered by default.

83 changes: 83 additions & 0 deletions PaddleLite-android-demo/camera_preview_demo/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import java.security.MessageDigest

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.baidu.paddle.lite.demo.camera_preview_demo"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
arguments '-DANDROID_PLATFORM=android-23', "-DANDROID_ARM_NEON=TRUE", '-DANDROID_STL=c++_shared', "-DANDROID_TOOLCHAIN="
abiFilters 'arm64-v8a'
cppFlags "-std=c++11"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

def archives = [
[
'src' : 'https://paddlelite-demo.bj.bcebos.com/libs/android/opencv-4.2.0-android-sdk.tar.gz',
'dest': 'OpenCV'
]
]

task downloadAndExtractArchives(type: DefaultTask) {
doFirst {
println "Downloading and extracting archives including libs and models"
}
doLast {
// Prepare cache folder for archives
String cachePath = "cache"
if (!file("${cachePath}").exists()) {
mkdir "${cachePath}"
}
archives.eachWithIndex { archive, index ->
MessageDigest messageDigest = MessageDigest.getInstance('MD5')
messageDigest.update(archive.src.bytes)
String cacheName = new BigInteger(1, messageDigest.digest()).toString(32)
// Download the target archive if not exists
boolean copyFiles = !file("${archive.dest}").exists()
if (!file("${cachePath}/${cacheName}.tar.gz").exists()) {
ant.get(src: archive.src, dest: file("${cachePath}/${cacheName}.tar.gz"))
copyFiles = true; // force to copy files from the latest archive files
}
// Extract the target archive if its dest path does not exists
if (copyFiles) {
copy {
from tarTree("${cachePath}/${cacheName}.tar.gz")
into "${archive.dest}"
}
}
}
}
}
preBuild.dependsOn downloadAndExtractArchives
21 changes: 21 additions & 0 deletions PaddleLite-android-demo/camera_preview_demo/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baidu.paddle.lite.demo.camera_preview;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.baidu.paddle.lite.demo", appContext.getPackageName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.baidu.paddle.lite.demo.camera_preview">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC or SHARED, and provides
# the relative paths to its source code. You can define multiple libraries, and
# CMake builds them for you. Gradle automatically packages shared libraries with
# your APK.

set(OpenCV_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/../../../OpenCV/sdk/native/jni")
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS})

set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -ffast-math -Ofast -Os -DNDEBUG -fno-exceptions -fomit-frame-pointer -fno-asynchronous-unwind-tables -fno-unwind-tables"
)
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden -fdata-sections -ffunction-sections"
)
set(CMAKE_SHARED_LINKER_FLAGS
"${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections -Wl,-z,nocopyreloc")

add_library(
# Sets the name of the library.
Native
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
Native.cc Pipeline.cc Utils.cc)

find_library(
# Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that you want CMake to locate.
log)

target_link_libraries(# Specifies the target library.
Native ${OpenCV_LIBS} GLESv2 EGL ${log-lib})

# Pack c++ runtime library into APK
if(${ANDROID_STL} MATCHES "_shared")
if("${ANDROID_STL}" STREQUAL "gnustl_shared")
set(so_dir "gnu-libstdc++/4.9")
set(so_name "gnustl_shared")
elseif("${ANDROID_STL}" STREQUAL "c++_shared")
set(so_dir "llvm-libc++")
set(so_name "c++_shared")
else()
message(FATAL_ERROR "ANDROID_STL=${ANDROID_STL} is not supported")
endif()
add_custom_command(
TARGET Native
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy
${ANDROID_NDK}/sources/cxx-stl/${so_dir}/libs/${ANDROID_ABI}/lib${so_name}.so
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${so_name}.so)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "Native.h"
#include "Pipeline.h"

#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_baidu_paddle_lite_demo_camera_preview_Native
* Method: nativeInit
* Signature:
* ()J
*/
JNIEXPORT jlong JNICALL
Java_com_baidu_paddle_lite_demo_camera_1preview_Native_nativeInit(JNIEnv *env,
jclass thiz) {
return reinterpret_cast<jlong>(new Pipeline());
}

/*
* Class: com_baidu_paddle_lite_demo_camera_preview_Native
* Method: nativeRelease
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL
Java_com_baidu_paddle_lite_demo_camera_1preview_Native_nativeRelease(
JNIEnv *env, jclass thiz, jlong ctx) {
if (ctx == 0) {
return JNI_FALSE;
}
Pipeline *pipeline = reinterpret_cast<Pipeline *>(ctx);
delete pipeline;
return JNI_TRUE;
}

/*
* Class: com_baidu_paddle_lite_demo_camera_preview_Native
* Method: nativeProcess
* Signature: (JIIIILjava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL
Java_com_baidu_paddle_lite_demo_camera_1preview_Native_nativeProcess(
JNIEnv *env, jclass thiz, jlong ctx, jint inTextureId, jint outTextureId,
jint textureWidth, jint textureHeight, jstring jsavedImagePath) {
if (ctx == 0) {
return JNI_FALSE;
}
std::string savedImagePath = jstring_to_cpp_string(env, jsavedImagePath);
Pipeline *pipeline = reinterpret_cast<Pipeline *>(ctx);
return pipeline->Process(inTextureId, outTextureId, textureWidth,
textureHeight, savedImagePath);
}

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit 90b684a

Please sign in to comment.