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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | package irdc.ex07_16_1; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.content.Context; import android.content.pm.ActivityInfo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.PointF; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.hardware.Camera.ShutterCallback; import android.media.FaceDetector; import android.media.FaceDetector.Face; import android.os.Bundle; import android.os.Environment; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.Toast; public class EX07_16_1 extends Activity implements SurfaceHolder.Callback { private Camera mCamera; private Button mButton,mButton1,mButton2; private SurfaceView mSurfaceView; private SurfaceHolder holder; private AutoFocusCallback mAutoFocusCallback = new AutoFocusCallback(); private String rootpath="/sdcard/test"; private Bitmap bmp; private int cnt=1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 隱藏狀態列 */ this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); /* 隱藏標題列 */ requestWindowFeature(Window.FEATURE_NO_TITLE); /* 設定螢幕顯示為橫向 */ this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setContentView(R.layout.main); /* SurfaceHolder設定 */ mSurfaceView = (SurfaceView) findViewById(R.id.mSurfaceView); holder = mSurfaceView.getHolder(); holder.addCallback(EX07_16_1.this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); /* Button初始化 */ mButton = (Button)findViewById(R.id.myButton); //mButton1 = (Button)findViewById(R.id.myButton1); //mButton2 = (Button)findViewById(R.id.myButton2); /* 拍照Button的事件處理 */ mButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { takePicture(); /* 自動對焦後拍照 */ //mCamera.autoFocus(mAutoFocusCallback); } }); /* 存檔Button的事件處理 */ /* mButton1.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { /* 儲存檔案 */ /* if(bmp!=null) { /* 檢視SDCard是否存在 */ /* if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { /* SD卡不存在,顯示Toast訊息 */ /* Toast.makeText(EX07_16_1.this,"SD卡不存在!無法儲存相片", Toast.LENGTH_LONG).show(); } else { try {*/ /* 資料夾不在就先建立 */ /* File f=new File ( Environment.getExternalStorageDirectory(),path ); if(!f.exists()) { f.mkdir(); } /* 儲存相片檔 */ /* File n=new File("face1.jpg"); FileOutputStream bos = new FileOutputStream(n.getAbsolutePath()); /* 檔案轉換 */ /* bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos); /* 呼叫flush()方法,更新BufferStream */ /* bos.flush(); /* 結束OutputStream */ /* bos.close(); } catch (Exception e) { e.printStackTrace(); } } }*/ mButton.setVisibility(View.VISIBLE); /* mButton1.setVisibility(View.GONE); mButton2.setVisibility(View.GONE);*/ /* 重新設定Camera */ stopCamera(); initCamera(); } ; /* 放棄Button的事件處理 */ /* mButton2.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { mButton.setVisibility(View.VISIBLE); mButton1.setVisibility(View.GONE); mButton2.setVisibility(View.GONE); /* 重新設定Camera */ /*stopCamera(); initCamera(); } }); }*/ @Override public void surfaceCreated(SurfaceHolder surfaceholder) { try { /* 打開相機, */ mCamera = Camera.open(); mCamera.setPreviewDisplay(holder); } catch (IOException exception) { mCamera.release(); mCamera = null; } } @Override public void surfaceChanged(SurfaceHolder surfaceholder, int format,int w,int h) { /* 相機初始化 */ initCamera(); } @Override public void surfaceDestroyed(SurfaceHolder surfaceholder) { stopCamera(); mCamera.release(); mCamera = null; } /* 拍照的method */ private void takePicture() { if (mCamera != null) { mCamera.takePicture(shutterCallback, rawCallback,jpegCallback); /* 儲存檔案 */ if(bmp!=null) { /* 檢視SDCard是否存在 */ if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { /* SD卡不存在,顯示Toast訊息 */ Toast.makeText(EX07_16_1.this,"SD卡不存在!無法儲存相片", Toast.LENGTH_LONG).show(); } else { try { /* 資料夾不在就先建立 */ File f=new File ( Environment.getExternalStorageDirectory(),rootpath ); if(!f.exists()) { f.mkdir(); } /* 儲存相片檔 */ File n=new File("face.jpg"); FileOutputStream bos = new FileOutputStream(n.getAbsolutePath()); /* 檔案轉換 */ bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos); /* 呼叫flush()方法,更新BufferStream */ bos.flush(); /* 結束OutputStream */ bos.close(); } catch (Exception e) { e.printStackTrace(); } } } } } private ShutterCallback shutterCallback = new ShutterCallback() { public void onShutter() { /* 按下快門瞬間會呼叫這裡的程式 */ } }; private PictureCallback rawCallback = new PictureCallback() { public void onPictureTaken(byte[] _data, Camera _camera) { /* 要處理raw data可寫在此 */ } }; private PictureCallback jpegCallback = new PictureCallback() { public void onPictureTaken(byte[] _data, Camera _camera) { /* 取得相片 */ try { /* 設定Button可視性 */ mButton.setVisibility(View.GONE); mButton1.setVisibility(View.VISIBLE); mButton2.setVisibility(View.VISIBLE); /* 取得相片Bitmap物件 */ bmp = BitmapFactory.decodeByteArray(_data, 0,_data.length); } catch (Exception e) { e.printStackTrace(); } } }; /* 自定義class AutoFocusCallback */ public final class AutoFocusCallback implements android.hardware.Camera.AutoFocusCallback { public void onAutoFocus(boolean focused, Camera camera) { /* 對到焦才拍照 */ //takePicture(); } }; /* 相機初始化的method */ private void initCamera() { if (mCamera != null) { try { Camera.Parameters parameters = mCamera.getParameters(); /* 設定相片大小為1024*768, 格式為JPG */ parameters.setPictureFormat(PixelFormat.JPEG); parameters.setPictureSize(1024,768); mCamera.setParameters(parameters); /* 開啟預覽畫面 */ mCamera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } } /* 停止相機的method */ private void stopCamera() { if (mCamera != null) { try { /* 停止預覽 */ mCamera.stopPreview(); } catch(Exception e) { e.printStackTrace(); } } } /*private class myView extends View{ private String rootpath="/sdcard/test/face1.jpg"; private int imageWidth, imageHeight; private int numberOfFace = 5; private FaceDetector myFaceDetect; private FaceDetector.Face[] myFace; float myEyesDistance; int numberOfFaceDetected; Bitmap myBitmap; public myView(Context context) { super(context); // TODO Auto-generated constructor stub BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; myBitmap = BitmapFactory.decodeFile(rootpath,options); imageWidth = myBitmap.getWidth(); imageHeight = myBitmap.getHeight(); myFace = new FaceDetector.Face[numberOfFace]; myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace); numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace); } protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub canvas.drawBitmap(myBitmap, 0, 0, null); Paint myPaint = new Paint(); myPaint.setColor(Color.GREEN); myPaint.setStyle(Paint.Style.STROKE); myPaint.setStrokeWidth(3); for(int i=0; i < numberOfFaceDetected; i++) { Face face = myFace[i]; PointF myMidPoint = new PointF(); face.getMidPoint(myMidPoint); myEyesDistance = face.eyesDistance(); canvas.drawRect( (int)(myMidPoint.x - myEyesDistance), (int)(myMidPoint.y - myEyesDistance), (int)(myMidPoint.x + myEyesDistance), (int)(myMidPoint.y + myEyesDistance), myPaint); } } }*/ } |
Direct link: https://paste.plurk.com/show/660437