android连接otg摄像头,Android系统OTG_usb链接摄像头的驱动和使用
這段時間做一個Android項目,需要在一臺Android 4.4.4系統(tǒng)設(shè)備上鏈接usb攝像頭,用作人臉識別,
于是作為Android項目組“骨干”(哈哈臭屁一下^-^)的我開始了usb攝像頭攻堅戰(zhàn),爬帖子,找博客,
最后眼睛瞅準(zhǔn)了一篇博客(感謝)
http://blog.csdn.net/sukhoi27smk/article/details/18269097
然后下載源碼開始了我的usb攝像頭驅(qū)動之旅
我整理的庫文件下載點擊查看
首先是在Linux Ubuntu系統(tǒng)下面配置環(huán)境,AS,NDK,如果想寫C代碼方便可以安裝QT(個人推薦,其他都行,比如gedit就很好使)
軟件安裝,環(huán)境配置,這些自當(dāng)不必多說。。。網(wǎng)上有N多種方式可以搞定
接下來就開始代碼改造
找到j(luò)ni文件夾下的ImageProc.c文件修改其中的jni接口方法名,使之指向CameraPreview.java
我修改后如下所示,找到其他的jni方法用同樣方式修改方法名(不要忘了同步.h文件中的方法名哦。。。):
1void Java_com_mojsoft_usbcamera_view_CameraPreview_pixeltobmp( JNIEnv* env,jobject thiz,jobject bitmap)
Ctrl+Alt+T打開終端,cd到你的jni目錄下,運行ndk-build,編譯成功
cd .. 回到j(luò)ni目錄外面,運行l(wèi)s就可以看到libs,obj兩個目錄,將libs中的libImageProc.so文件拷貝到你項目中的jniLibs目錄下(AS)
接下來是java調(diào)用 [注意:java包名和C文件方法格式一致]
將CameraPreview文件放到布局中,編譯運行。。。
接下來就是盯著屏幕。。。
滿滿的驚喜,問題就這么解決了,哈哈
下面是主要的CameraSurfaView類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163class CameraPreview extends SurfaceView implements SurfaceHolder.Callback, Runnable{
private static final boolean DEBUG = true;
private static final String TAG = "WebCam";
protected Context context;
private SurfaceHolder holder;
Thread mainLoop = null;
private Bitmap bmp = null;
private byte[] byteArrary;
private boolean cameraExists = false;
private boolean shouldStop = false;
// /dev/videox (x=cameraId+cameraBase) is used.
// In some omap devices, system uses /dev/video[0-3],
// so users must use /dev/video[4-].
// In such a case, try cameraId=0 and cameraBase=4
private int cameraId = 0;
private int cameraBase = 0;
// This definition also exists in ImageProc.h.
// Webcam must support the resolution 640x480 with YUYV format.
static final int IMG_WIDTH = 640;
static final int IMG_HEIGHT = 480;
// The following variables are used to draw camera images.
private int winWidth = 0;
private int winHeight = 0;
private Rect rect;
private int dw, dh;
private float rate;
private boolean isFramAready = false;
private AlreadyRGBListener listener;
// JNI functions
public native int prepareCamera(int videoid);
public native int prepareCameraWithBase(int videoid, int camerabase);
public native void processCamera();
public native void stopCamera();
public native void pixeltobmp(Bitmap bitmap);
public native byte[] getRgb();
static {
System.loadLibrary("ImageProc");
}
public CameraPreview(Context context){
super(context);
this.context = context;
if (DEBUG) Log.d(TAG, "CameraPreview constructed");
setFocusable(true);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}
public CameraPreview(Context context, AttributeSet attrs){
super(context, attrs);
this.context = context;
if (DEBUG) Log.d(TAG, "CameraPreview constructed");
setFocusable(true);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}
@Override
public void run(){
while (true && cameraExists) {
//obtaining display area to draw a large image
if (winWidth == 0) {
winWidth = this.getWidth();
winHeight = this.getHeight();
if (winWidth * 3 / 4 <= winHeight) {
dw = 0;
dh = (winHeight - winWidth * 3 / 4) / 2;
rate = ((float) winWidth) / IMG_WIDTH;
rect = new Rect(dw, dh, dw + winWidth - 1, dh + winWidth * 3 / 4 - 1);
} else {
dw = (winWidth - winHeight * 4 / 3) / 2;
dh = 0;
rate = ((float) winHeight) / IMG_HEIGHT;
rect = new Rect(dw, dh, dw + winHeight * 4 / 3 - 1, dh + winHeight - 1);
}
}
// obtaining a camera image (pixel data are stored in an array in JNI).
processCamera();
// camera image to bmp
pixeltobmp(bmp);
byte[] arr = getRgb();
isFramAready = true;
Canvas canvas = getHolder().lockCanvas();
if (canvas != null) {
// draw camera bmp on canvas
canvas.drawBitmap(bmp, null, rect, null);
getHolder().unlockCanvasAndPost(canvas);
}
listener.alreadyRGB(arr);
if (shouldStop) {
shouldStop = false;
break;
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder){
if (DEBUG) Log.d(TAG, "surfaceCreated");
if (bmp == null) {
bmp = Bitmap.createBitmap(IMG_WIDTH, IMG_HEIGHT, Bitmap.Config.ARGB_8888);
}
if (byteArrary == null) {
byteArrary = new byte[IMG_WIDTH * IMG_HEIGHT];
}
// /dev/videox (x=cameraId + cameraBase) is used
//下面一句是原代碼,我嘗試失敗,于是我將其修改為 int ret = prepareCamera(0);后成功調(diào)用
// int ret = prepareCameraWithBase(cameraId, cameraBase);
int ret = prepareCamera(0);
if (ret != -1) cameraExists = true;
mainLoop = new Thread(this);
mainLoop.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
if (DEBUG) Log.d(TAG, "surfaceChanged");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
if (DEBUG) Log.d(TAG, "surfaceDestroyed");
if (cameraExists) {
shouldStop = true;
while (shouldStop) {
try {
Thread.sleep(50); // wait for thread stopping
} catch (Exception e) {
}
}
}
stopCamera();
}
public void setGetBitmapListener(AlreadyRGBListener listener){
this.listener = listener;
}
}
總結(jié)
以上是生活随笔為你收集整理的android连接otg摄像头,Android系统OTG_usb链接摄像头的驱动和使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 太简单了!串口触摸屏开发HMI的全流程介
- 下一篇: 工作之余,请IT人员开怀大笑吧