日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android连接otg摄像头,Android系统OTG_usb链接摄像头的驱动和使用

發布時間:2023/12/8 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android连接otg摄像头,Android系统OTG_usb链接摄像头的驱动和使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這段時間做一個Android項目,需要在一臺Android 4.4.4系統設備上鏈接usb攝像頭,用作人臉識別,

于是作為Android項目組“骨干”(哈哈臭屁一下^-^)的我開始了usb攝像頭攻堅戰,爬帖子,找博客,

最后眼睛瞅準了一篇博客(感謝)

http://blog.csdn.net/sukhoi27smk/article/details/18269097

然后下載源碼開始了我的usb攝像頭驅動之旅

我整理的庫文件下載點擊查看

首先是在Linux Ubuntu系統下面配置環境,AS,NDK,如果想寫C代碼方便可以安裝QT(個人推薦,其他都行,比如gedit就很好使)

軟件安裝,環境配置,這些自當不必多說。。。網上有N多種方式可以搞定

接下來就開始代碼改造

找到jni文件夾下的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 .. 回到jni目錄外面,運行ls就可以看到libs,obj兩個目錄,將libs中的libImageProc.so文件拷貝到你項目中的jniLibs目錄下(AS)

接下來是java調用 [注意: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);后成功調用

// 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;

}

}

總結

以上是生活随笔為你收集整理的android连接otg摄像头,Android系统OTG_usb链接摄像头的驱动和使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。