Skip to content

Commit 02939c4

Browse files
committed
add x86 ABI support
1 parent c9925dd commit 02939c4

File tree

100 files changed

+5769
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+5769
-25
lines changed

Application.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
APP_STL := gnustl_static
22
APP_CPPFLAGS := -frtti -fexceptions
33
APP_PLATFORM := android-21
4-
APP_ABI += armeabi-v7a
4+
APP_ABI += armeabi-v7a x86
55

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ opencv341 android sdk with contrib341 module build on windows.
33

44
天知道在windows上编译这俩个库要趟过多少坑。
55

6-
工作需要,目前主要编译了armeabi-v7a的.so库.a库,主要用于jni编程,附带java应用层函数。
6+
工作需要,目前主要编译了armeabi-v7a和x86的.so库.a库,主要用于jni编程,附带java应用层函数。
77

88

99
## A templet build.gradle for armeabi-v7a with NDK
@@ -18,7 +18,7 @@ android {
1818
ndk {
1919
moduleName "opencv3"
2020
ldLibs "log", "jnigraphics", "m", "z"
21-
abiFilters "armeabi-v7a"
21+
abiFilters "armeabi-v7a","x86"
2222
}
2323
//配置SO文件存放路径
2424
sourceSets {

build.gradle

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,17 @@ dependencies {
127127
}
128128

129129

130-
131-
/* A templet build.gradle for armeabi-v7a with NDK
132-
130+
/*使用ndk的配置模板
133131
apply plugin: 'com.android.library'
134132
android {
135-
compileSdkVersion 23
136-
133+
...
137134
defaultConfig {
138-
minSdkVersion 21
139-
targetSdkVersion 23
140-
versionCode 1
141-
versionName "1.0"
135+
...
142136
//配置NDK信息
143137
ndk {
144138
moduleName "opencv3"
145139
ldLibs "log", "jnigraphics", "m", "z"
146-
abiFilters "armeabi-v7a"
140+
abiFilters "armeabi-v7a","x86"
147141
}
148142
//配置SO文件存放路径
149143
sourceSets {
@@ -153,21 +147,12 @@ android {
153147
}
154148
}
155149
}
156-
buildTypes {
157-
release {
158-
minifyEnabled false
159-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
160-
}
161-
}
162150
//设置构建脚本路径,会自动索引同目录下的Application.mk
163151
externalNativeBuild {
164152
ndkBuild {
165153
path 'src/main/jni/Android.mk'
166154
}
167155
}
168156
}
169-
dependencies {
170-
implementation fileTree(dir: 'libs', include: ['*.jar'])
171-
implementation 'com.android.support:appcompat-v7:23.4.0'
172-
}
157+
...
173158
*/

java/project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212

1313
android.library=true
1414
# Project target.
15-
target=android-14
15+
target=android-23
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
package org.opencv.android;
2+
3+
import java.util.Arrays;
4+
import java.util.concurrent.Semaphore;
5+
import java.util.concurrent.TimeUnit;
6+
import android.annotation.TargetApi;
7+
import android.content.Context;
8+
import android.graphics.SurfaceTexture;
9+
import android.hardware.camera2.CameraAccessException;
10+
import android.hardware.camera2.CameraCaptureSession;
11+
import android.hardware.camera2.CameraCharacteristics;
12+
import android.hardware.camera2.CameraDevice;
13+
import android.hardware.camera2.CameraManager;
14+
import android.hardware.camera2.CaptureRequest;
15+
import android.hardware.camera2.params.StreamConfigurationMap;
16+
import android.os.Handler;
17+
import android.os.HandlerThread;
18+
import android.util.Log;
19+
import android.util.Size;
20+
import android.view.Surface;
21+
22+
@TargetApi(21)
23+
public class Camera2Renderer extends CameraGLRendererBase {
24+
25+
protected final String LOGTAG = "Camera2Renderer";
26+
private CameraDevice mCameraDevice;
27+
private CameraCaptureSession mCaptureSession;
28+
private CaptureRequest.Builder mPreviewRequestBuilder;
29+
private String mCameraID;
30+
private Size mPreviewSize = new Size(-1, -1);
31+
32+
private HandlerThread mBackgroundThread;
33+
private Handler mBackgroundHandler;
34+
private Semaphore mCameraOpenCloseLock = new Semaphore(1);
35+
36+
Camera2Renderer(CameraGLSurfaceView view) {
37+
super(view);
38+
}
39+
40+
@Override
41+
protected void doStart() {
42+
Log.d(LOGTAG, "doStart");
43+
startBackgroundThread();
44+
super.doStart();
45+
}
46+
47+
48+
@Override
49+
protected void doStop() {
50+
Log.d(LOGTAG, "doStop");
51+
super.doStop();
52+
stopBackgroundThread();
53+
}
54+
55+
boolean cacPreviewSize(final int width, final int height) {
56+
Log.i(LOGTAG, "cacPreviewSize: "+width+"x"+height);
57+
if(mCameraID == null) {
58+
Log.e(LOGTAG, "Camera isn't initialized!");
59+
return false;
60+
}
61+
CameraManager manager = (CameraManager) mView.getContext()
62+
.getSystemService(Context.CAMERA_SERVICE);
63+
try {
64+
CameraCharacteristics characteristics = manager
65+
.getCameraCharacteristics(mCameraID);
66+
StreamConfigurationMap map = characteristics
67+
.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
68+
int bestWidth = 0, bestHeight = 0;
69+
float aspect = (float)width / height;
70+
for (Size psize : map.getOutputSizes(SurfaceTexture.class)) {
71+
int w = psize.getWidth(), h = psize.getHeight();
72+
Log.d(LOGTAG, "trying size: "+w+"x"+h);
73+
if ( width >= w && height >= h &&
74+
bestWidth <= w && bestHeight <= h &&
75+
Math.abs(aspect - (float)w/h) < 0.2 ) {
76+
bestWidth = w;
77+
bestHeight = h;
78+
}
79+
}
80+
Log.i(LOGTAG, "best size: "+bestWidth+"x"+bestHeight);
81+
if( bestWidth == 0 || bestHeight == 0 ||
82+
mPreviewSize.getWidth() == bestWidth &&
83+
mPreviewSize.getHeight() == bestHeight )
84+
return false;
85+
else {
86+
mPreviewSize = new Size(bestWidth, bestHeight);
87+
return true;
88+
}
89+
} catch (CameraAccessException e) {
90+
Log.e(LOGTAG, "cacPreviewSize - Camera Access Exception");
91+
} catch (IllegalArgumentException e) {
92+
Log.e(LOGTAG, "cacPreviewSize - Illegal Argument Exception");
93+
} catch (SecurityException e) {
94+
Log.e(LOGTAG, "cacPreviewSize - Security Exception");
95+
}
96+
return false;
97+
}
98+
99+
@Override
100+
protected void openCamera(int id) {
101+
Log.i(LOGTAG, "openCamera");
102+
CameraManager manager = (CameraManager) mView.getContext().getSystemService(Context.CAMERA_SERVICE);
103+
try {
104+
String camList[] = manager.getCameraIdList();
105+
if(camList.length == 0) {
106+
Log.e(LOGTAG, "Error: camera isn't detected.");
107+
return;
108+
}
109+
if(id == CameraBridgeViewBase.CAMERA_ID_ANY) {
110+
mCameraID = camList[0];
111+
} else {
112+
for (String cameraID : camList) {
113+
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraID);
114+
if( id == CameraBridgeViewBase.CAMERA_ID_BACK &&
115+
characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK ||
116+
id == CameraBridgeViewBase.CAMERA_ID_FRONT &&
117+
characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT) {
118+
mCameraID = cameraID;
119+
break;
120+
}
121+
}
122+
}
123+
if(mCameraID != null) {
124+
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
125+
throw new RuntimeException(
126+
"Time out waiting to lock camera opening.");
127+
}
128+
Log.i(LOGTAG, "Opening camera: " + mCameraID);
129+
manager.openCamera(mCameraID, mStateCallback, mBackgroundHandler);
130+
}
131+
} catch (CameraAccessException e) {
132+
Log.e(LOGTAG, "OpenCamera - Camera Access Exception");
133+
} catch (IllegalArgumentException e) {
134+
Log.e(LOGTAG, "OpenCamera - Illegal Argument Exception");
135+
} catch (SecurityException e) {
136+
Log.e(LOGTAG, "OpenCamera - Security Exception");
137+
} catch (InterruptedException e) {
138+
Log.e(LOGTAG, "OpenCamera - Interrupted Exception");
139+
}
140+
}
141+
142+
@Override
143+
protected void closeCamera() {
144+
Log.i(LOGTAG, "closeCamera");
145+
try {
146+
mCameraOpenCloseLock.acquire();
147+
if (null != mCaptureSession) {
148+
mCaptureSession.close();
149+
mCaptureSession = null;
150+
}
151+
if (null != mCameraDevice) {
152+
mCameraDevice.close();
153+
mCameraDevice = null;
154+
}
155+
} catch (InterruptedException e) {
156+
throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
157+
} finally {
158+
mCameraOpenCloseLock.release();
159+
}
160+
}
161+
162+
private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
163+
164+
@Override
165+
public void onOpened(CameraDevice cameraDevice) {
166+
mCameraDevice = cameraDevice;
167+
mCameraOpenCloseLock.release();
168+
createCameraPreviewSession();
169+
}
170+
171+
@Override
172+
public void onDisconnected(CameraDevice cameraDevice) {
173+
cameraDevice.close();
174+
mCameraDevice = null;
175+
mCameraOpenCloseLock.release();
176+
}
177+
178+
@Override
179+
public void onError(CameraDevice cameraDevice, int error) {
180+
cameraDevice.close();
181+
mCameraDevice = null;
182+
mCameraOpenCloseLock.release();
183+
}
184+
185+
};
186+
187+
private void createCameraPreviewSession() {
188+
int w=mPreviewSize.getWidth(), h=mPreviewSize.getHeight();
189+
Log.i(LOGTAG, "createCameraPreviewSession("+w+"x"+h+")");
190+
if(w<0 || h<0)
191+
return;
192+
try {
193+
mCameraOpenCloseLock.acquire();
194+
if (null == mCameraDevice) {
195+
mCameraOpenCloseLock.release();
196+
Log.e(LOGTAG, "createCameraPreviewSession: camera isn't opened");
197+
return;
198+
}
199+
if (null != mCaptureSession) {
200+
mCameraOpenCloseLock.release();
201+
Log.e(LOGTAG, "createCameraPreviewSession: mCaptureSession is already started");
202+
return;
203+
}
204+
if(null == mSTexture) {
205+
mCameraOpenCloseLock.release();
206+
Log.e(LOGTAG, "createCameraPreviewSession: preview SurfaceTexture is null");
207+
return;
208+
}
209+
mSTexture.setDefaultBufferSize(w, h);
210+
211+
Surface surface = new Surface(mSTexture);
212+
213+
mPreviewRequestBuilder = mCameraDevice
214+
.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
215+
mPreviewRequestBuilder.addTarget(surface);
216+
217+
mCameraDevice.createCaptureSession(Arrays.asList(surface),
218+
new CameraCaptureSession.StateCallback() {
219+
@Override
220+
public void onConfigured( CameraCaptureSession cameraCaptureSession) {
221+
mCaptureSession = cameraCaptureSession;
222+
try {
223+
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
224+
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
225+
226+
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, mBackgroundHandler);
227+
Log.i(LOGTAG, "CameraPreviewSession has been started");
228+
} catch (CameraAccessException e) {
229+
Log.e(LOGTAG, "createCaptureSession failed");
230+
}
231+
mCameraOpenCloseLock.release();
232+
}
233+
234+
@Override
235+
public void onConfigureFailed(
236+
CameraCaptureSession cameraCaptureSession) {
237+
Log.e(LOGTAG, "createCameraPreviewSession failed");
238+
mCameraOpenCloseLock.release();
239+
}
240+
}, mBackgroundHandler);
241+
} catch (CameraAccessException e) {
242+
Log.e(LOGTAG, "createCameraPreviewSession");
243+
} catch (InterruptedException e) {
244+
throw new RuntimeException(
245+
"Interrupted while createCameraPreviewSession", e);
246+
}
247+
finally {
248+
//mCameraOpenCloseLock.release();
249+
}
250+
}
251+
252+
private void startBackgroundThread() {
253+
Log.i(LOGTAG, "startBackgroundThread");
254+
stopBackgroundThread();
255+
mBackgroundThread = new HandlerThread("CameraBackground");
256+
mBackgroundThread.start();
257+
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
258+
}
259+
260+
private void stopBackgroundThread() {
261+
Log.i(LOGTAG, "stopBackgroundThread");
262+
if(mBackgroundThread == null)
263+
return;
264+
mBackgroundThread.quitSafely();
265+
try {
266+
mBackgroundThread.join();
267+
mBackgroundThread = null;
268+
mBackgroundHandler = null;
269+
} catch (InterruptedException e) {
270+
Log.e(LOGTAG, "stopBackgroundThread");
271+
}
272+
}
273+
274+
@Override
275+
protected void setCameraPreviewSize(int width, int height) {
276+
Log.i(LOGTAG, "setCameraPreviewSize("+width+"x"+height+")");
277+
if(mMaxCameraWidth > 0 && mMaxCameraWidth < width) width = mMaxCameraWidth;
278+
if(mMaxCameraHeight > 0 && mMaxCameraHeight < height) height = mMaxCameraHeight;
279+
try {
280+
mCameraOpenCloseLock.acquire();
281+
282+
boolean needReconfig = cacPreviewSize(width, height);
283+
mCameraWidth = mPreviewSize.getWidth();
284+
mCameraHeight = mPreviewSize.getHeight();
285+
286+
if( !needReconfig ) {
287+
mCameraOpenCloseLock.release();
288+
return;
289+
}
290+
if (null != mCaptureSession) {
291+
Log.d(LOGTAG, "closing existing previewSession");
292+
mCaptureSession.close();
293+
mCaptureSession = null;
294+
}
295+
mCameraOpenCloseLock.release();
296+
createCameraPreviewSession();
297+
} catch (InterruptedException e) {
298+
mCameraOpenCloseLock.release();
299+
throw new RuntimeException("Interrupted while setCameraPreviewSize.", e);
300+
}
301+
}
302+
}

0 commit comments

Comments
 (0)