Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit 8a575d7

Browse files
committed
Enable the reading from assets.
1 parent b195718 commit 8a575d7

File tree

11 files changed

+322
-220
lines changed

11 files changed

+322
-220
lines changed

Demo/Android/AICamera/app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ find_package(Paddle)
2626

2727
set(SRC_FILES src/main/cpp/image_recognizer_jni.cpp
2828
src/main/cpp/paddle_image_recognizer.cpp
29+
src/main/cpp/binary_reader.cpp
2930
src/main/cpp/image_utils.cpp)
3031

3132
set(LINK_FLAGS "-Wl,--version-script ${CMAKE_CURRENT_SOURCE_DIR}/paddle_jni.map")

Demo/Android/AICamera/app/build.gradle

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ android {
55
buildToolsVersion "26.0.1"
66
defaultConfig {
77
applicationId "com.paddlepaddle.aicamera"
8-
minSdkVersion 16
8+
minSdkVersion 19
99
targetSdkVersion 25
1010
versionCode 1
1111
versionName "1.0"
@@ -15,9 +15,7 @@ android {
1515
}
1616
externalNativeBuild {
1717
cmake {
18-
arguments "-DANDROID_TOOLCHAIN=gcc",
19-
"-DANDROID_ARM_NEON=FALSE",
20-
"-DCMAKE_VERBOSE_MAKEFILE=1"
18+
arguments "-DCMAKE_VERBOSE_MAKEFILE=1"
2119
cppFlags "-O3 -std=c++11 -frtti -fexceptions"
2220
}
2321
}
@@ -53,4 +51,4 @@ dependencies {
5351
compile 'com.android.support:appcompat-v7:25.3.1'
5452
compile 'com.android.support.constraint:constraint-layout:1.0.2'
5553
testCompile 'junit:junit:4.12'
56-
}
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#ifndef ANDROID_COMMON_H
16+
#define ANDROID_COMMON_H
17+
18+
#include <android/asset_manager.h>
19+
#include <android/log.h>
20+
#include <stdlib.h>
21+
22+
#define TAG "ImageRecognizer"
23+
24+
#define LOGI(format, ...) \
25+
__android_log_print(ANDROID_LOG_INFO, TAG, format, ##__VA_ARGS__)
26+
#define LOGW(format, ...) \
27+
__android_log_print(ANDROID_LOG_WARN, TAG, format, ##__VA_ARGS__)
28+
#define LOGE(format, ...) \
29+
__android_log_print(ANDROID_LOG_ERROR, TAG, "Error: " format, ##__VA_ARGS__)
30+
31+
#endif // ANDROID_COMMON_H
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include <stdlib.h>
16+
17+
#include "binary_reader.h"
18+
19+
#define TAG "BinaryReader"
20+
21+
#ifdef __ANDROID__
22+
#include <android/log.h>
23+
#define LOGI(format, ...) \
24+
__android_log_print(ANDROID_LOG_INFO, TAG, format, ##__VA_ARGS__)
25+
#define LOGW(format, ...) \
26+
__android_log_print(ANDROID_LOG_WARN, TAG, format, ##__VA_ARGS__)
27+
#define LOGE(format, ...) \
28+
__android_log_print(ANDROID_LOG_ERROR, TAG, "Error: " format, ##__VA_ARGS__)
29+
#else
30+
#include <stdio.h>
31+
#define LOGI(format, ...) \
32+
fprintf(stdout, "[" TAG "]" format "\n", ##__VA_ARGS__)
33+
#define LOGW(format, ...) \
34+
fprintf(stdout, "[" TAG "]" format "\n", ##__VA_ARGS__)
35+
#define LOGE(format, ...) \
36+
fprintf(stderr, "[" TAG "]Error: " format "\n", ##__VA_ARGS__)
37+
#endif
38+
39+
void* BinaryReader::read_binary_from_external(const char* filename,
40+
long* size) {
41+
FILE* file = fopen(filename, "rb");
42+
if (file == nullptr) {
43+
LOGE("%s open failure.", filename);
44+
return nullptr;
45+
}
46+
47+
fseek(file, 0L, SEEK_END);
48+
*size = ftell(file);
49+
fseek(file, 0L, SEEK_SET);
50+
51+
void* buf = malloc(*size);
52+
if (buf == nullptr) {
53+
LOGE("memory allocation failure, size %ld.", *size);
54+
return nullptr;
55+
}
56+
57+
fread(buf, 1, *size, file);
58+
59+
fclose(file);
60+
file = nullptr;
61+
62+
return buf;
63+
}
64+
65+
#ifdef __ANDROID__
66+
AAssetManager* BinaryReader::aasset_manager_(nullptr);
67+
68+
void* BinaryReader::read_binary_from_asset(const char* filename, long* size) {
69+
if (aasset_manager_ != nullptr) {
70+
AAsset* asset =
71+
AAssetManager_open(aasset_manager_, filename, AASSET_MODE_STREAMING);
72+
73+
if (asset != nullptr) {
74+
*size = AAsset_getLength(asset);
75+
76+
void* buf = (char*)malloc(*size);
77+
if (buf == nullptr) {
78+
LOGW("memory allocation failure, size %ld", *size);
79+
return nullptr;
80+
}
81+
82+
if (AAsset_read(asset, buf, *size) > 0) {
83+
AAsset_close(asset);
84+
return buf;
85+
} else {
86+
LOGW("read %s failure, size %ld.", filename, *size);
87+
}
88+
89+
AAsset_close(asset);
90+
asset = nullptr;
91+
} else {
92+
LOGW("%s does not exist in assets.", filename);
93+
}
94+
}
95+
96+
return nullptr;
97+
}
98+
#endif
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
17+
#ifdef __ANDROID__
18+
#include <android/asset_manager.h>
19+
#endif
20+
21+
class BinaryReader {
22+
public:
23+
void* operator()(const char* filename, long* size) {
24+
#ifdef __ANDROID__
25+
void* buf = read_binary_from_asset(filename, size);
26+
#else
27+
void* buf = NULL;
28+
#endif
29+
if (buf == NULL) {
30+
buf = read_binary_from_external(filename, size);
31+
}
32+
return buf;
33+
}
34+
35+
private:
36+
void* read_binary_from_external(const char* filename, long* size);
37+
38+
#ifdef __ANDROID__
39+
private:
40+
void* read_binary_from_asset(const char* filename, long* size);
41+
42+
public:
43+
static void set_aasset_manager(AAssetManager* aasset_manager) {
44+
aasset_manager_ = aasset_manager;
45+
}
46+
47+
private:
48+
static AAssetManager* aasset_manager_;
49+
#endif
50+
};

Demo/Android/AICamera/app/src/main/cpp/common.h

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)