自制Android相机
生活随笔
收集整理的這篇文章主要介紹了
自制Android相机
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
首先,Camera中,調(diào)用系統(tǒng)相機(jī):
使用系統(tǒng)Intent,ACTION_IMAGE_CAPTURE
注冊Camera功能
<intent-filter><action android:name="android.media.action.IMAGE_CAPTURE"/><category android:name="android.intent.category.DEFAULT"/> </intent-filter>?調(diào)用系統(tǒng)的Camera功能,由于默認(rèn)返回的是縮略圖,以下方法是通過將拍的照片存入自定義的SD卡中,之后再取出到ImageView中,使得返回的為原圖: MyFilePath = Environment.getExternalStorageDirectory().getPath(); MyFilePath = MyFilePath + "/" + "example.png";public void StartCamera(View view){Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);Uri photoUri = Uri.fromFile(new File(MyFilePath));//將系統(tǒng)默認(rèn)保存路徑改為指定的sd卡中的路徑intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);startActivityForResult(intent, REQUEST); }protected void onActivityResult(int requestCode, int resultCode, Intent data){super.onActivityResult(requestCode, requestCode, data);if(resultCode == RESULT_OK){if(requestCode == REQUEST){Bundle bundle = data.getExtras();Bitmap bitmap = (Bitmap)bundle.get("data");mImageView.setImageBitmap(bitmap);}else{FileInputStream fis = null;try{fis = new FileInputStream(MyFilePath);Bitmap bitmap = BitmapFactory.decodeStream(fis);mImageView.setImageBitmap(bitmap);//這樣顯示的就是原圖,而不是縮略圖}catch(FileNotFoundException e){e.printStackTrace();}finally{try{fis.close();}catch(IOException e){e.printStackTrace();}}}} }以下就是自定義相機(jī)的基礎(chǔ)實(shí)現(xiàn)
首先需要注意,由于是自定義的相機(jī),就需要添加相機(jī)權(quán)限:
<uses-permission android:name="android.permission.CAMERA">然后,即開始創(chuàng)建自定義的相機(jī): public class CustomCamera extends Activity implements SurfaceHolder.Callback{private Camera mCamera;private SurfaceView mPreview;//要實(shí)現(xiàn)SurfaceHolder.Callback接口中的方法private SurfaceHolder mHolder;private Camera.PictureCallback mpictureCallback = new Camera.PictureCallback(){public void onPictureTaken(byte[] data, Camera camera) {File tempFile = new File("/sdcard/temp.png")try{FileOutputStream fos = new FileOutputStream(tempFile);fos.write(data);fos.close();Intent intent = new Intent(CustomCamera.this, ResultAty.class);Intent.putExtra("picPath", temFile.getAbsolutePath());startActivity(intent);CustomCamera.this.finish();}catch(Exception e){e.printStackTrace();}} };protected void OnCreate(Bundle savedInstanceState){super.OnCreate(savedInstanceState);setContentView(R.layout.custom);mPreview = (SurfaceView)findViewById(R.id.preview);mHolder = mPreview.getHolder();mHolder.addCallback(this);mPreview.setOnClickListener(new View.onClickListener(){public void onClick(View v){mCamera.autoFocus(null);}});}public void capturePhoto(View view){Camera.parameters parameters = mCamera.getParameters();parameters.setPictureFormat(ImageFormat.JPEG);parameters.setPreviewSize(800,400);//調(diào)整拍照的顯示效果parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);mCamera.autoFocus(new Camera.AutoFocusCallback(){public void onAutoFocus(boolean success, Camera camera){if(success){mCamera.takePicture(null, null, mPictureCallback);}}}); }//------------------------ protected void onResume(){super.onResume();if(mCamera == null){mCamera = getCamera();if(mHolder != null){setStartPreview(mCamera, mHolder);}} }protected void onPause(){super.onPause();releaseCamera(); } //----------通過重寫這兩個方法,使得Camera生命周期與Activity周期進(jìn)行綁定 //以保證資源能夠被正確的初始化和釋放//--------創(chuàng)建了Camera生命周期的三個方法: //獲取Camera對象,同時進(jìn)行初始化操作 private Camera getCamera(){Camera camera;//在Android5.0之后系統(tǒng)推薦使用Camera2//但不使用高級的功能時,Camera仍然適用//在此是最基本的Camera操作try{camera = Camera.open();//進(jìn)行初始化操作}catch(Exception e){camera = null;e.printStackTrace();}return camera; }//開始預(yù)覽相機(jī)內(nèi)容 private void setStartPreview(Camera camera, SurfaceHolder holder){try{camera.setPreviewDisplay(holder);//將Camera與SurfaceView進(jìn)行關(guān)聯(lián)//同時Camera進(jìn)行取景,預(yù)覽畫面camera.setDisplayOrientation(90);//將Camera預(yù)覽旋轉(zhuǎn)90度camera.startPreview();}catch(IOException e){e.printStackTrace();} }//釋放相機(jī)資源,同時解除與SurfaceView的綁定 private void releaseCamera(){if(mCamera != null){mCamera.setPreviewCallback(null);mCamera.stopPreview();mCamera.release();mCamera = null;} } //---------至此,camera生命周期的方法創(chuàng)建完畢 //-----以下將Camera生命周期與SurfaceView進(jìn)行綁定 public void surfaceCreated(SurfaceHolder holder){setStartPreview(mCamera, mHolder); }public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){mCamera.stopPreview();setStartPreview(mCamera, mHolder); }public void surfaceDestroyed(SurfaceHolder holder){releaseCamera(); } //------------對相機(jī)的生命周期的管理//還需要另外一個類: public class ResultAty extends Activity{protected void OnCreate(Bundle savedInstanceState){super.OnCreate(savedInstanceState);setContentView(R.layout.result);String path = getIntent().getStringExtra("picPath");ImageView imageView = (ImageView)findViewById(R.id.pic);// Bitmap bitmap = BitmapFactory.decodeFile(path);// imageView.setImageBitmap(bitmap);try{FileInputStream fis = new FileInputStream(path);Bitmap bitmap = BitmapFactory.decodeStream(fis);Matrix matrix = new Matrix();matrix.setRotate(90);//取得的照片進(jìn)行旋轉(zhuǎn)bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);imageView.setImageBitmap(bitmap);}catch(Exception e){e.printStackTrace();}} }另外,若想要給相機(jī)添加一個水印功能,只需要在xml文件中,在<SurfaceView>標(biāo)簽中添加<TextView>標(biāo)簽即可,很簡單,不予演示了。
總結(jié)
以上是生活随笔為你收集整理的自制Android相机的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 接上,优化滚动的效率
- 下一篇: Android自制SwitchBar(附