質問編集履歴

2

追加

2017/12/02 07:39

投稿

ths
ths

スコア21

test CHANGED
File without changes
test CHANGED
@@ -16,6 +16,322 @@
16
16
 
17
17
  補足
18
18
 
19
+ ソースコードです
20
+
21
+ AutoCamera.java
22
+
23
+ ```ここに言語を入力
24
+
25
+ package a.camera;
26
+
27
+ import android.app.Activity;
28
+
29
+ import android.bluetooth.BluetoothAdapter;
30
+
31
+ import android.graphics.Bitmap;
32
+
33
+ import android.graphics.BitmapFactory;
34
+
35
+ import android.hardware.Camera;
36
+
37
+ import android.os.Bundle;
38
+
39
+ import android.os.Environment;
40
+
41
+ import android.os.Handler;
42
+
43
+ import android.util.Log;
44
+
45
+ import android.view.SurfaceHolder;
46
+
47
+ import android.view.SurfaceView;
48
+
49
+ import android.view.View;
50
+
51
+ import android.view.Window;
52
+
53
+ import android.view.WindowManager;
54
+
55
+ import android.widget.Toast;
56
+
57
+
58
+
59
+ import java.io.FileOutputStream;
60
+
61
+
62
+
63
+ public class AutoCamera extends Activity {
64
+
65
+ protected void onCreate(Bundle savedInstanceState) {
66
+
67
+ super.onCreate(savedInstanceState);
68
+
69
+ requestWindowFeature(Window.FEATURE_NO_TITLE);
70
+
71
+ setContentView(new CameraView(this));
72
+
73
+ getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
74
+
75
+ }
76
+
77
+ }
78
+
79
+ ```
80
+
81
+
82
+
83
+ CameraPreview.java
84
+
85
+ ```ここに言語を入力
86
+
87
+ package a.camera;
88
+
89
+
90
+
91
+ import android.annotation.TargetApi;
92
+
93
+ import android.content.Context;
94
+
95
+ import android.graphics.PixelFormat;
96
+
97
+ import android.hardware.Camera;
98
+
99
+ import android.os.Build;
100
+
101
+ import android.os.Handler;
102
+
103
+ import android.util.Log;
104
+
105
+ import android.view.SurfaceHolder;
106
+
107
+ import android.view.SurfaceView;
108
+
109
+ import android.widget.Toast;
110
+
111
+
112
+
113
+ import java.io.BufferedOutputStream;
114
+
115
+ import java.net.Socket;
116
+
117
+
118
+
119
+ import static android.content.ContentValues.TAG;
120
+
121
+
122
+
123
+ public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
124
+
125
+ private SurfaceHolder holder;
126
+
127
+ private Camera camera;
128
+
129
+ private static final int WIDTH = 480;
130
+
131
+ private static final int HEIGHT = 320;
132
+
133
+ private static final int PORT = 49152;
134
+
135
+ private static final String IP_ADDR = "192.168.11.2";
136
+
137
+ private static boolean is_save;
138
+
139
+
140
+
141
+ public CameraView(Context context) {
142
+
143
+ super(context);
144
+
145
+ holder=getHolder();
146
+
147
+ holder.addCallback(this);
148
+
149
+ holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
150
+
151
+ }
152
+
153
+
154
+
155
+ @Override
156
+
157
+ public void surfaceCreated(SurfaceHolder holder) {
158
+
159
+ try {
160
+
161
+ camera = Camera.open(0);
162
+
163
+ camera.setPreviewDisplay(holder);
164
+
165
+ } catch (Exception e) {
166
+
167
+ }
168
+
169
+ }
170
+
171
+
172
+
173
+ @Override
174
+
175
+ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
176
+
177
+ Camera.Parameters parameters=camera.getParameters();
178
+
179
+ parameters.setPictureSize(WIDTH, HEIGHT);
180
+
181
+ camera.setParameters(parameters);
182
+
183
+ camera.startPreview();
184
+
185
+ faceDetection();
186
+
187
+ }
188
+
189
+
190
+
191
+ @Override
192
+
193
+ public void surfaceDestroyed(SurfaceHolder holder) {
194
+
195
+ camera.setPreviewCallback(null);
196
+
197
+ camera.stopPreview();
198
+
199
+ camera.release();
200
+
201
+ camera=null;
202
+
203
+ }
204
+
205
+
206
+
207
+ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
208
+
209
+ public void faceDetection(){
210
+
211
+ final int standardFaceScore = 60000;
212
+
213
+ try {camera.startFaceDetection();
214
+
215
+ } catch (IllegalArgumentException e) {
216
+
217
+ } catch (RuntimeException e) {}
218
+
219
+
220
+
221
+ camera.setFaceDetectionListener(new Camera.FaceDetectionListener() {
222
+
223
+ @Override
224
+
225
+ public void onFaceDetection(Camera.Face[] faces, Camera camera) {
226
+
227
+ Log.d(TAG, "faces count: " + faces.length);
228
+
229
+ for (Camera.Face face : faces) {
230
+
231
+ Log.d(TAG, "face id: " + face.id);
232
+
233
+ Log.d(TAG, "face score: " + face.score);
234
+
235
+ Log.d(TAG, "face rect: " + face.rect.left + "," + face.rect.top + " - "
236
+
237
+ + face.rect.right + "," + face.rect.bottom);
238
+
239
+ if (face.mouth != null) {
240
+
241
+ Log.d(TAG, "face mouth: " + face.mouth.x + "," + face.mouth.y);
242
+
243
+ Log.d(TAG, "face leftEye: " + face.leftEye.x + "," + face.leftEye.y);
244
+
245
+ Log.d(TAG, "face rightEye: " + face.rightEye.x + "," + face.rightEye.y);
246
+
247
+ }
248
+
249
+ if(face.score > standardFaceScore && !is_save){
250
+
251
+ takePicture();
252
+
253
+ }
254
+
255
+ }
256
+
257
+ }
258
+
259
+ });
260
+
261
+ }
262
+
263
+
264
+
265
+ public void takePicture() {
266
+
267
+ // カメラのスクリーンショットの取得
268
+
269
+ camera.takePicture(null, null,new Camera.PictureCallback() {
270
+
271
+ public void onPictureTaken(byte[] data,Camera camera) {
272
+
273
+ Toast.makeText(getContext(), "撮影しました", Toast.LENGTH_LONG).show();
274
+
275
+ is_save = true;
276
+
277
+ sendData(getContext(), data);
278
+
279
+ new Handler().postDelayed(new Runnable() {
280
+
281
+ @Override
282
+
283
+ public void run() {
284
+
285
+ is_save = false;
286
+
287
+ }
288
+
289
+ }, 3000);
290
+
291
+ }
292
+
293
+ });
294
+
295
+ }
296
+
297
+
298
+
299
+ private void sendData(Context context, byte[] data){
300
+
301
+ // ソケットの作成
302
+
303
+ Socket socket;
304
+
305
+ BufferedOutputStream out;
306
+
307
+ try{
308
+
309
+ socket = new Socket(IP_ADDR, PORT);
310
+
311
+ out = new BufferedOutputStream(socket.getOutputStream());
312
+
313
+ out.write(data);
314
+
315
+ if(out != null) out.close();
316
+
317
+ if(socket != null) socket.close();
318
+
319
+ } catch (Exception ex){
320
+
321
+ ex.printStackTrace();
322
+
323
+ }
324
+
325
+ }
326
+
327
+ }
328
+
329
+
330
+
331
+ ```
332
+
333
+
334
+
19
335
  基本このエラーです。
20
336
 
21
337
  ```ここに言語を入力

1

質問の追加

2017/12/02 07:39

投稿

ths
ths

スコア21

test CHANGED
File without changes
test CHANGED
@@ -11,3 +11,15 @@
11
11
  ちゃんとカメラのパーミッションを許可してます。
12
12
 
13
13
  どなたか原因分かる方いらっしゃいましたらご教授お願い致します。。
14
+
15
+
16
+
17
+ 補足
18
+
19
+ 基本このエラーです。
20
+
21
+ ```ここに言語を入力
22
+
23
+ 12-02 16:16:33.598 1305-17957/? E/HCEOutdoorUtil: (413) int HCEView_ManagerSensor::getIdent() Error(ALooper_pollAll returns TIMEOUT. Set Initialized Values)
24
+
25
+ ```