huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package com.DeviceTest;
 
import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
 
import com.DeviceTest.helper.ConfigUtil;
import com.DeviceTest.helper.ControlButtonUtil;
 
import java.io.FileOutputStream;
import android.content.Intent;
 
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager; 
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.os.Handler;
import android.os.Message;
import android.view.Surface;
import android.widget.ImageView;
import android.content.pm.PackageManager;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.app.ActivityManager;
import java.lang.reflect.Method;
 
 
public class CameraBackTestActivity extends Activity{
   
       private static final String TAG="CameraWithFlashTestActivity";
 
       private static final int MSG_TAKE_OVER = 1;
       private static final int MSG_TAKE_ERROR = 2;
       private TextView tv_prompt;
       private ImageView imageView;
       private boolean isTakeStat=false; //FOR ESC key 
       private boolean isCanTake=true; //if in taking process,click preview screen should not call take photo again!
//        private int  CameraCount=0;
//        private int  testCount=0;
       private Bitmap bitmap;
       private  int testCameraID=0;
        private boolean mCameraPrepared;
       public void onCreate(Bundle bundle) {
               super.onCreate(bundle);
               getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
               getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
               requestWindowFeature(Window.FEATURE_NO_TITLE);
             //  setContentView(new CameraView(this)); 
             setContentView(R.layout.camerawithflashtest);
             tv_prompt=(TextView)findViewById(R.id.CameratextMsg);
           //  tv_prompt.setText("请点击屏幕打开后置摄像头\n然后点击预览屏幕拍照");
             tv_prompt.setText(getString(R.string.CameraFlashtextBack));
             ControlButtonUtil.initControlButtonView(this);
//            CameraCount=getCameraCount();
           if(isAppInstalled(this, "com.example.cam")) {
               Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.cam");
               if( intent != null ) {
                   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                   startActivity(intent);
               }
               new Handler().postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       stopAppByForce(CameraBackTestActivity.this,"com.example.cam");
                   }
               }, 5000);                
           }                                        
       }
       private void stopAppByForce(Context context , String packageName) {
           ActivityManager mActivityManager = (ActivityManager)
                   context.getSystemService(Context.ACTIVITY_SERVICE);
           Method method = null;
           try {
               method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
               method.invoke(mActivityManager, packageName);
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
       protected void onResume() {
           super.onResume();
//            Log.d(TAG,"onResume... CameraCount="+CameraCount);
            //findViewById(R.id.btn_Pass).setVisibility(View.INVISIBLE);    
       //     findViewById(R.id.btn_Pass).setEnabled(false);
            isTakeStat=false;
            isCanTake=true;
//             testCount=0;
            
       }
 
       public boolean isAppInstalled(Context context, String packageName) {
           PackageManager packageManager = context.getPackageManager();
           try {
                   packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
                   android.util.Log.d("RRRRRRRR", "com.example.cam can use");
                   return true;
               } catch (PackageManager.NameNotFoundException e) {
                   return false;
           }
       }
 
       public boolean onTouchEvent(MotionEvent paramMotionEvent) {
           if(isCanTake){
               if (paramMotionEvent.getAction() == MotionEvent.ACTION_DOWN) {
 
                   if(isAppInstalled(this, "com.example.cam")) {
                       android.util.Log.d("RRRRRRRR", "com.example.cam");
                       Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.cam");
                       if( intent != null ) {
                           intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                           startActivity(intent);
                           return true;
                       }
                       Log.d("RRRRR", "CameraExample Not Available");
                   }
 
                   setContentView(new CameraView(this,testCameraID)); 
                   testCameraID=0;
                   isTakeStat=true;
                  }
               }  
                   return super.onTouchEvent(paramMotionEvent);      
           }
 
       public boolean dispatchKeyEvent(KeyEvent event) {
           //open camera maybe error,allow user back.
           if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
               if(isTakeStat)
               {
                   myHandler.sendEmptyMessage(MSG_TAKE_OVER);
               }
               else
                   return false;
           }
           return super.dispatchKeyEvent(event);
       }
 
//         private int getCameraCount()
//           {  
//                int cameraCount = 0;  
//                Camera.CameraInfo cameraInfo = new Camera.CameraInfo();  
//                cameraCount = Camera.getNumberOfCameras(); // get cameras number    c
//                return cameraCount;  
//            } 
          
       Handler myHandler = new Handler() {
               public void handleMessage(Message msg) {
                   switch (msg.what) {
                   case MSG_TAKE_OVER:
                       setContentView(R.layout.cameratestv);
                       ControlButtonUtil.initControlButtonView(CameraBackTestActivity.this);
                       imageView=(ImageView) findViewById(R.id.imgV);
                       imageView.setImageBitmap(bitmap);
                       //Log.i("lzx", "-------------bitmap----"+bitmap.toString());
                       findViewById(R.id.btn_Pass).setVisibility(View.VISIBLE);
                   //    findViewById(R.id.btn_Pass).setEnabled(true);
                       break;
                   case MSG_TAKE_ERROR:
                           isTakeStat=false;
                           isCanTake=true;
//                            testCount=0;
                           setContentView(R.layout.camerawithflashtest);
                           
                            ControlButtonUtil.initControlButtonView(CameraBackTestActivity.this);
                             //findViewById(R.id.btn_Pass).setVisibility(View.INVISIBLE);
                        if (!mCameraPrepared) {
                            findViewById(R.id.btn_Fail).performClick();
                        }
                           //findViewById(R.id.btn_Pass).setEnabled(false);
                       break;
                       
                   }
                   super.handleMessage(msg);
               }
           };
 
       public class CameraView extends SurfaceView implements SurfaceHolder.Callback, Camera.PictureCallback {
           private SurfaceHolder holder;
           private Camera camera;
           private boolean af;
           private int cameraId;
           
           public CameraView(Context context) {
               super(context);
               Log.d(TAG,"constructed...");
               holder = getHolder();
               holder.addCallback(this);
        
               //holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
           }
 
           public CameraView(Context context,int camera_Id) {
               super(context);
               Log.d(TAG,"constructed... 2");
               holder = getHolder();
               holder.addCallback(this);
               cameraId=camera_Id;
        
               //holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//
           }
        
           public void surfaceCreated(SurfaceHolder holder) {//
               try {
                   
                   camera = Camera.open(cameraId);
                   Log.d(TAG,"surfaceCreated:camera!=null is:"+(camera!=null)+",cameraId="+cameraId);
                   camera.setPreviewDisplay(holder);
//                    if (cameraId == 0 && !ConfigUtil.isSpecialEvb()) {
//                        Camera.Parameters parameters = camera.getParameters();
//                        //parameters.setPreviewSize(width, height);
//                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);   
//                        camera.setParameters(parameters);
//                    }
                   setRightCameraOrientation(cameraId, camera);
                   camera.startPreview();
                   mCameraPrepared = true;
               } catch (Exception e) {
                       Log.w(TAG,"surfaceCreated e:"+e.getMessage());
                    myHandler.sendEmptyMessage(MSG_TAKE_ERROR);
               }
           }
        
           @Override
           public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
               
           }
        
           public void surfaceDestroyed(SurfaceHolder holder) {
               if(null != camera)
               {
                   camera.setPreviewCallback(null);
                   camera.stopPreview();
                   camera.release();
                   camera = null;
               }
               Log.d(TAG,"surfaceDestroyed");
           }
        
           @Override
           public boolean onTouchEvent(MotionEvent event) {
                if (!mCameraPrepared) {
                    Log.d(TAG,"!mCameraPrepared not do onTouchEvent");
                    return true;
                }
               if (event.getAction() == MotionEvent.ACTION_DOWN) {
                   Log.d(TAG,"MotionEvent.ACTION_DOWN");
                   if(null != camera)
                   {
                       camera.autoFocus(null);
                       af = true;
                       Log.i("lzx", "-------------------------touch event---------------------");
                   }
                   else
                   {
                       
                       myHandler.sendEmptyMessage(MSG_TAKE_ERROR);
                   }
               }
               if (event.getAction() == MotionEvent.ACTION_UP){
                   Log.d(TAG,"event.getAction() == MotionEvent.ACTION_UP");
                   if( af == true && isCanTake) {
                       camera.takePicture(null, null, this);
                       isCanTake=false;
                       af = false;
                   
                   }
               }
               return true;
           }
        
           public void onPictureTaken(byte[] data, Camera camera) {
               try {
               isTakeStat=false;
               isCanTake=false;
               bitmap= BitmapFactory.decodeByteArray(data, 0, data.length);
           } catch (Exception e) {
           }
       //    camera.startPreview();
 
           myHandler.sendEmptyMessage(MSG_TAKE_OVER);
           Log.i("lzx", "-------------picture-bitmap---"+bitmap.toString());
           }
        
           private void data2file(byte[] w, String fileName) throws Exception {
               FileOutputStream out = null;
               try {
                   out = new FileOutputStream(fileName);
                   out.write(w);
                   out.close();
               } catch (Exception e) {
                   if (out != null)
                       out.close();
                   throw e;
               }
           }
        
       }
       private void setRightCameraOrientation(int cameraId, Camera mCamera) {
           Camera.CameraInfo info = new Camera.CameraInfo();
           Camera.getCameraInfo(cameraId, info);
           int rotation = this.getWindowManager().getDefaultDisplay()
                   .getRotation();
           int degrees = 0;
           switch (rotation) {
               case Surface.ROTATION_0:
                   degrees = 0;
                   break;
               case Surface.ROTATION_90:
                   degrees = 90;
                   break;
               case Surface.ROTATION_180:
                   degrees = 180;
                   break;
               case Surface.ROTATION_270:
                   degrees = 270;
                   break;
           }
           int result;
           if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
               result = (info.orientation + degrees) % 360;
               result = (360 - result) % 360; // compensate the mirror
           } else { // back-facing
               result = (info.orientation - degrees + 360) % 360;
           }
           mCamera.setDisplayOrientation(result);
       }
   }