質問編集履歴
2
発生している問題の詳細を変更した
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,6 +21,11 @@
|
|
21
21
|
|
22
22
|
```
|
23
23
|
グローバル変数の取得が一度しか出来ない
|
24
|
+
|
25
|
+
※2/13 追記
|
26
|
+
CameraViewのcntsvとCommonのkeyyyが変化する処理がずっとループしており、
|
27
|
+
再度getInt()することが出来ない
|
28
|
+
|
24
29
|
```
|
25
30
|
|
26
31
|
### 該当のソースコード
|
1
setIntを用いるCameraViewを記入し忘れていました、申し訳ありません。 助長になっていますが、最後のところにcommon.setInt(cntsv)があります。
title
CHANGED
File without changes
|
body
CHANGED
@@ -97,8 +97,204 @@
|
|
97
97
|
}
|
98
98
|
```
|
99
99
|
|
100
|
+
```Java
|
101
|
+
public class CameraView extends SurfaceView
|
102
|
+
implements SurfaceHolder.Callback, Camera.PreviewCallback {
|
103
|
+
//カメラの縦横 640*480は標準的に動く
|
104
|
+
private static final int PREVIEW_WIDTH = 640;
|
105
|
+
private static final int PREVIEW_HEIGHT = 480;
|
106
|
+
private static final int FRAME_WIDTH = 50;
|
107
|
+
private static final int SCALE = 1;
|
108
|
+
private static final int VIEW_POINTS = 200;
|
109
|
+
private static final float PEN_WIDTH = 3.0F;
|
110
|
+
private int mBufSize;
|
111
|
+
private float[] mVal;
|
112
|
+
private int mSamples;
|
113
|
+
private int mTop;
|
114
|
+
private int mStep;
|
115
|
+
private Camera mCamera = null;
|
116
|
+
private SurfaceHolder mHolder = null;
|
117
|
+
private SurfaceTexture mSurfaceTexture = null;
|
118
|
+
String bbb ="hello_world";
|
119
|
+
int keyfin = 0;
|
120
|
+
int cntsv ;
|
121
|
+
Timer timer;
|
100
122
|
|
101
123
|
|
124
|
+
Common common;
|
125
|
+
public int coun = 0;
|
126
|
+
public Paint paintf;
|
102
127
|
|
128
|
+
public Activity activity;
|
129
|
+
|
130
|
+
//CameraViewのメソッド
|
131
|
+
public CameraView(Context context) {
|
132
|
+
super(context);
|
133
|
+
|
134
|
+
mHolder = getHolder();
|
135
|
+
mHolder.addCallback(this);
|
136
|
+
|
137
|
+
//ここでインスタンス化
|
138
|
+
common = (Common)context.getApplicationContext();
|
139
|
+
timer = new Timer();
|
140
|
+
}
|
141
|
+
|
142
|
+
//カメラ作成にはsurfaceが必要で、surfaceの作成
|
143
|
+
public void surfaceCreated(SurfaceHolder holder) {
|
144
|
+
// TODO Auto-generated method stub
|
145
|
+
|
146
|
+
//getWidthは画面サイズ取得
|
147
|
+
mBufSize = getWidth();
|
148
|
+
mVal = new float[mBufSize];
|
149
|
+
mStep = (mBufSize - FRAME_WIDTH * 2) / VIEW_POINTS;
|
150
|
+
|
151
|
+
// カメラオープン(カメラのインスタンスを取得)
|
152
|
+
mCamera = Camera.open();
|
153
|
+
try {
|
154
|
+
mCamera.setPreviewDisplay(holder);
|
155
|
+
|
156
|
+
} catch (IOException e) {
|
157
|
+
// TODO Auto-generated catch block
|
158
|
+
e.printStackTrace();
|
159
|
+
|
160
|
+
}
|
161
|
+
}
|
162
|
+
|
163
|
+
//surface変更時の処理
|
164
|
+
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
|
165
|
+
// TODO Auto-generated method stub
|
166
|
+
stopPreview();
|
167
|
+
// プレビュー画面のサイズ設定
|
168
|
+
Camera.Parameters params = mCamera.getParameters();
|
169
|
+
params.setPreviewSize(PREVIEW_WIDTH, PREVIEW_HEIGHT);
|
170
|
+
// params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
|
171
|
+
mCamera.setParameters(params);
|
172
|
+
// プレビュー開始
|
173
|
+
mTop = 0;
|
174
|
+
mSamples = 0;
|
175
|
+
startPreview();
|
176
|
+
}
|
177
|
+
|
178
|
+
//surface破棄時の処理
|
179
|
+
public void surfaceDestroyed(SurfaceHolder holder) {
|
180
|
+
// TODO Auto-generated method stub
|
181
|
+
stopPreview();
|
182
|
+
mCamera.release();
|
183
|
+
mCamera = null;
|
184
|
+
}
|
185
|
+
|
186
|
+
//ここでdataを定義
|
187
|
+
@Override
|
188
|
+
public void onPreviewFrame(byte[] data, Camera camera) {
|
189
|
+
// TODO Auto-generated method stub
|
190
|
+
|
191
|
+
mVal[mSamples % mBufSize] = calcLuminance(data);
|
192
|
+
|
193
|
+
// else {
|
194
|
+
mSamples++;
|
195
|
+
// drawLuminance();
|
196
|
+
}
|
197
|
+
|
198
|
+
// プレビュー開始
|
199
|
+
private void startPreview() {
|
200
|
+
mCamera.setPreviewCallback(this);
|
201
|
+
mCamera.startPreview();
|
202
|
+
}
|
203
|
+
|
204
|
+
// プレビュー停止
|
205
|
+
private void stopPreview() {
|
206
|
+
mCamera.setPreviewCallback(null);
|
207
|
+
mCamera.stopPreview();
|
208
|
+
}
|
209
|
+
|
210
|
+
// 輝度値の計算 "n"を箱として10個作る
|
211
|
+
float n[] = new float[10];
|
212
|
+
|
213
|
+
private float calcLuminance(byte[] data) {
|
214
|
+
//時間計測開始
|
215
|
+
long start = System.currentTimeMillis();
|
216
|
+
float sumVal1 = 0.0f;
|
217
|
+
float sumVal2 = 0.0f;
|
218
|
+
float sumVal3 = 0.0f;
|
219
|
+
float sumVal4 = 0.0f;
|
220
|
+
|
221
|
+
//cntはcountするだけのもの
|
222
|
+
int cnt = 0;
|
223
|
+
int cnt1 = 0;
|
224
|
+
int cnt2 = 0;
|
225
|
+
int cnt3 = 0;
|
226
|
+
int cnt4 = 0;
|
227
|
+
|
228
|
+
//この範囲からこの範囲での、輝度値を積分している
|
229
|
+
for (int y = PREVIEW_HEIGHT / 4 ; y < PREVIEW_HEIGHT * 3 / 8 ; y++) {
|
230
|
+
//widthを1/4(160)から3/4(480)まで回す
|
231
|
+
for (int x = PREVIEW_WIDTH / 4; x < PREVIEW_WIDTH * 3 / 8; x++) {
|
232
|
+
|
233
|
+
//1/4~3/4は範囲を設定しただけ(端末の大きさにより、変化するため)
|
234
|
+
//sumValは、画素値の合計値
|
235
|
+
|
236
|
+
sumVal1 = sumVal1 + (float) (data[y * PREVIEW_WIDTH + x] & 0xff);
|
237
|
+
cnt1++;
|
238
|
+
|
239
|
+
}
|
240
|
+
}
|
241
|
+
|
242
|
+
//2回目
|
243
|
+
for (int y = PREVIEW_HEIGHT / 4 ; y < PREVIEW_HEIGHT * 3 / 8 ; y++) {
|
244
|
+
for (int x = PREVIEW_WIDTH * 3 / 8; x < PREVIEW_WIDTH * 3 / 4; x++) {
|
245
|
+
|
246
|
+
sumVal2 = sumVal2 + (float) (data[y * PREVIEW_WIDTH + x] & 0xff);
|
247
|
+
cnt2++;
|
248
|
+
|
249
|
+
}
|
250
|
+
}
|
251
|
+
|
252
|
+
//3回目
|
253
|
+
for (int y = PREVIEW_HEIGHT * 3 / 8 ; y < PREVIEW_HEIGHT * 3 /4 ; y++) {
|
254
|
+
for (int x = PREVIEW_WIDTH / 4; x < PREVIEW_WIDTH * 3 / 8; x++) {
|
255
|
+
|
256
|
+
sumVal3 = sumVal3 + (float) (data[y * PREVIEW_WIDTH + x] & 0xff);
|
257
|
+
cnt3++;
|
258
|
+
|
259
|
+
}
|
260
|
+
}
|
261
|
+
|
262
|
+
//4回目
|
263
|
+
for (int y = PREVIEW_HEIGHT * 3/ 8 ; y < PREVIEW_HEIGHT * 3 / 4 ; y++) {
|
264
|
+
for (int x = PREVIEW_WIDTH *3 / 8; x < PREVIEW_WIDTH * 3 / 4; x++) {
|
265
|
+
|
266
|
+
sumVal4 = sumVal4 + (float) (data[y * PREVIEW_WIDTH + x] & 0xff);
|
267
|
+
cnt4++;
|
268
|
+
|
269
|
+
}
|
270
|
+
}
|
271
|
+
|
272
|
+
//合計値を、平均化している
|
273
|
+
sumVal1 = sumVal1/cnt1;
|
274
|
+
sumVal2 = sumVal2/cnt2;
|
275
|
+
sumVal3 = sumVal3/cnt3;
|
276
|
+
sumVal4 = sumVal4/cnt4;
|
277
|
+
|
278
|
+
//最大値を求めるコード
|
279
|
+
float sumValarray[]={sumVal1,sumVal2,sumVal3,sumVal4};
|
280
|
+
//とりあえず0を入れた
|
281
|
+
float sumValmax=sumValarray[0];
|
282
|
+
|
283
|
+
//sumValmaxを特定するループ
|
284
|
+
for(int i=0;i<sumValarray.length;i++){
|
285
|
+
if(sumValarray[i]>=sumValmax){
|
286
|
+
sumValmax= sumValarray[i];
|
287
|
+
//keyを取得するためのcntsv
|
288
|
+
cntsv=i;
|
289
|
+
}
|
290
|
+
|
291
|
+
// グローバル変数のset
|
292
|
+
common.setInt(cntsv);
|
293
|
+
|
294
|
+
}
|
295
|
+
```
|
296
|
+
|
297
|
+
|
298
|
+
|
103
299
|
### 試したこと
|
104
300
|
Timerを用いて、一定時間ごとの処理
|