This repository has been archived by the owner on Jan 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
DeviceOperationTest.java
235 lines (194 loc) · 6.85 KB
/
DeviceOperationTest.java
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package ndcam;
import android.graphics.ImageFormat;
import android.hardware.camera2.CameraCharacteristics;
import android.media.Image;
import android.media.ImageReader;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import java.nio.ByteBuffer;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
/**
* @author [email protected]
*/
@RunWith(AndroidJUnit4.class)
public class DeviceOperationTest extends CameraModelTest {
// Image reader doesn't have timeout. So we have to rule it for this test
@Rule
public Timeout timeout = new Timeout(60, TimeUnit.SECONDS);
ImageReader reader;
Device camera;
@Before
public void CreateImageReader() {
// 1920 * 1080, 30 FPS, YCbCr 4:2:0(YUV_420_888)
reader = ImageReader.newInstance( // create a new one
1920, 1080, // width, height
ImageFormat.YUV_420_888, // YCbCr
30 // reserve some images
);
Assert.assertNotNull(reader);
}
@Before
public void AcquireDevice() {
CameraModel.Init();
Device[] devices = CameraModel.GetDevices();
Assert.assertNotNull(devices);
camera = null;
// get rear camera for test
for (Device device : devices)
if (device.facing() == CameraCharacteristics.LENS_FACING_BACK)
camera = device;
Assert.assertNotNull(camera);
}
@After
public void CloseReaderAndDevice() throws Exception {
Assert.assertNotNull(reader);
reader.close();
Thread.sleep(500);
Assert.assertNotNull(camera);
camera.close();
// wait for camera framework to stop completely
Thread.sleep(500);
}
/**
* This scenario will generate error logs like the following one queueBuffer:
* BufferQueue has been abandoned
*/
@Test
public void StopRepeatWithoutConsume() throws Exception {
// start repeating capture operation
camera.repeat(reader.getSurface());
// Android Camera 2 API uses background thread.
// Give some time to the framework.
Thread.sleep(100);
camera.stopRepeat();
}
@Test
public void TryRepeatCapture() throws Exception {
// start repeating capture operation
camera.repeat(reader.getSurface());
Thread.sleep(100);
Image image = null;
int i = 0, count = 0;
while (i++ < 100) // try 100 capture (repeat mode)
{
// expect 30 FPS...
Thread.sleep(30);
// Fetch image 1 by 1.
image = reader.acquireNextImage();
if (image == null)
continue;
Log.v("ndk_camera", String.format("format %d width %d height %d timestamp %d", image.getFormat(),
image.getWidth(), image.getHeight(), image.getTimestamp()));
image.close();
count += 1;
}
camera.stopRepeat(); // stop after iteration
Assert.assertNotNull(image); // ensure at least 1 image was acquired
Assert.assertTrue(count > 1); // Repeating capture leads to multiple images
Assert.assertTrue(i > count); // !!! some images might be dropped !!!
}
/**
* This scenario will generate error logs like the following one queueBuffer:
* BufferQueue has been abandoned
*/
@Test
public void StopCaptureWithoutConsume() throws Exception {
// start repeating capture operation
camera.capture(reader.getSurface());
// Android Camera 2 API uses background thread.
// Give some time to the framework.
Thread.sleep(100);
camera.stopCapture();
}
@Test
public void TryCapture() throws Exception {
// start capture operation
camera.capture(reader.getSurface());
Thread.sleep(100);
// expect image in 2 sec
Image image = WaitForImage(executorService, reader).get(2, TimeUnit.SECONDS);
Assert.assertNotNull(image);
camera.stopCapture(); // stop after capture
Log.d("ndk_camera", String.format("format %d width %d height %d timestamp %d", image.getFormat(),
image.getWidth(), image.getHeight(), image.getTimestamp()));
image.close();
}
@Test
public void ReadImageDataToByteArray() throws Exception {
// start capture operation
camera.capture(reader.getSurface());
Thread.sleep(100);
// expect image in 2 sec
Image image = WaitForImage(executorService, reader).get(2, TimeUnit.SECONDS);
Assert.assertNotNull(image);
// stop after capture
camera.stopCapture();
Image.Plane[] planes = image.getPlanes();
Assert.assertNotNull(planes);
Assert.assertTrue(planes.length == 3);
int planeSize = image.getWidth() * image.getHeight();
int channel = planes.length;
Assert.assertTrue(image.getFormat() == ImageFormat.YUV_420_888);
//
// Checked with monospace fonts.
// D2Coding, Source Code Pro, Ubuntu Mono
//
// ◯ : U+25EF // y
// △ : U+25B3 // cb
// ▢ : U+25A2 // cr
//
// y ◯ ◯ ◯ ◯
// ◯ ◯ ◯ ◯
// ◯ ◯ ◯ ◯
// ◯ ◯ ◯ ◯
ByteBuffer yView = planes[0].getBuffer();
// `ccView1` & `ccView2` shared *same memory* but their range is different...
// cb/cr △ ▢ △ ▢
// △ ▢ △
ByteBuffer ccView1 = planes[1].getBuffer();
// cb/cr ▢ △ ▢
// △ ▢ △ ▢
ByteBuffer ccView2 = planes[2].getBuffer();
//
// ◯ : U+25EF // empty
// △ : U+25B3 // filling
// ▢ : U+25A2 // filled
//
byte[] yccData = new byte[planeSize * channel / 2];
// y △ △ △ △
// △ △ △ △
// △ △ △ △
// △ △ △ △
// cb/cr ◯ ◯ ◯ ◯
// ◯ ◯ ◯ ◯
yView.get(yccData, 0, planeSize);
// y ▢ ▢ ▢ ▢
// ▢ ▢ ▢ ▢
// ▢ ▢ ▢ ▢
// ▢ ▢ ▢ ▢
// cb/cr △ ◯ ◯ ◯
// ◯ ◯ ◯ ◯
ccView1.get(yccData, planeSize, 1);
// y ▢ ▢ ▢ ▢
// ▢ ▢ ▢ ▢
// ▢ ▢ ▢ ▢
// ▢ ▢ ▢ ▢
// cb/cr ▢ △ △ △
// △ △ △ △
ccView2.get(yccData, planeSize + 1, ccView2.remaining());
image.close();
}
}