編輯:關於Android編程
一、概述
本章節主要講述的Android相機Camera的相關知識點,主要包含三個方面,Android啟動系統相機生成縮略圖、Android啟動系統相機生成原圖以及Android自定義相機等。
二、Android啟動系統相機
核心代碼:
package com.czhappy.camerademo.activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; import com.czhappy.camerademo.R; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class MainActivity extends AppCompatActivity { private static int REQ_1 = 1; private static int REQ_2 = 2; private ImageView image_iv; private String mFilePath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image_iv = (ImageView) findViewById(R.id.image_iv); mFilePath = Environment.getExternalStorageDirectory().getPath(); mFilePath = mFilePath +"/" + System.currentTimeMillis()+".jpg"; } public void startCamera1(View view){ //隱式Intent調用 Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(it, REQ_1); } public void startCamera2(View view){ //隱式Intent調用 Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri photoUri = Uri.fromFile(new File(mFilePath)); it.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(it, REQ_2); } public void customCamera(View view){ Intent it = new Intent(MainActivity.this, CustomCameraActivity.class); startActivity(it); } /** * * @param requestCode * @param resultCode * @param data //縮略圖 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK){ if(requestCode == REQ_1){//縮略圖 Bundle bundle = data.getExtras(); Bitmap bmp = (Bitmap) bundle.get("data"); image_iv.setImageBitmap(bmp); }else if(requestCode == REQ_2){//原圖 FileInputStream fis = null; try { fis = new FileInputStream(mFilePath); Bitmap bitmap = BitmapFactory.decodeStream(fis); image_iv.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } } }
三、Android自定義相機
1.自定義相機步驟:
創建相機
創建SurfaceView
關聯相機與SurfaceView
調整相機顯示效果
自定義相機預覽界面
2.核心代碼:
自定義相機CustomCameraActivity.java
package com.czhappy.camerademo.activity; import android.content.Intent; import android.graphics.ImageFormat; import android.hardware.Camera; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import com.czhappy.camerademo.R; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * Description: * User: chenzheng * Date: 2016/12/27 0027 * Time: 13:43 */ public class CustomCameraActivity extends AppCompatActivity implements SurfaceHolder.Callback{ private SurfaceView mPreview; private Camera mCamera; private SurfaceHolder mHolder; private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { @Override 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 it = new Intent(CustomCameraActivity.this, CaptureResultActivity.class); it.putExtra("picPath", tempFile.getAbsolutePath()); startActivity(it); finish(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_camera); mPreview = (SurfaceView) this.findViewById(R.id.preview); mHolder = mPreview.getHolder(); mHolder.addCallback(this); //點擊自動對焦 mPreview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mCamera.autoFocus(null);; } }); } /** * 拍照 * @param view */ public void capture(View view){ Camera.Parameters parameters = mCamera.getParameters(); parameters.setPictureFormat(ImageFormat.JPEG); parameters.setPictureSize(800,400); parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);//自動對焦 mCamera.autoFocus(new Camera.AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { //對焦准確拍攝照片 if(success){ mCamera.takePicture(null, null, mPictureCallback); } } }); } /** * 獲取相機對象 * @return */ private Camera getCamera(){ mCamera = Camera.open(); return mCamera; } /** * 開始預覽相機內容 */ private void setStartPreview(Camera camera, SurfaceHolder holder){ try { camera.setPreviewDisplay(holder); camera.setDisplayOrientation(90);//設置相機預覽為豎屏90度 camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } /** * 釋放相機資源 */ private void releaseCamera(){ if(mCamera!=null){ mCamera.setPreviewCallback(null); mCamera.stopPreview(); mCamera.release(); mCamera = null; } } @Override protected void onResume() { super.onResume(); if (mCamera==null){ mCamera = getCamera(); if(mHolder!=null){ setStartPreview(mCamera, mHolder); } } } @Override protected void onPause() { super.onPause(); releaseCamera(); } @Override public void surfaceCreated(SurfaceHolder holder) { setStartPreview(mCamera, mHolder); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { mCamera.stopPreview(); setStartPreview(mCamera, mHolder); } @Override public void surfaceDestroyed(SurfaceHolder holder) { releaseCamera(); } }
生成圖片結果頁面CaptureResultActivity.java
package com.czhappy.camerademo.activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; import com.czhappy.camerademo.R; import java.io.FileInputStream; import java.io.FileNotFoundException; /** * Description: * User: chenzheng * Date: 2016/12/30 0030 * Time: 13:27 */ public class CaptureResultActivity extends AppCompatActivity { private String picPath; private ImageView result_iv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_capture_result); picPath = getIntent().getStringExtra("picPath"); result_iv = (ImageView) findViewById(R.id.result_iv); try { FileInputStream fis = new FileInputStream(picPath); Bitmap bitmap = BitmapFactory.decodeStream(fis); Matrix matrix = new Matrix(); matrix.setRotate(90);//旋轉90度矩陣 bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); result_iv.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
四、項目源代碼下載
http://xiazai.jb51.net/201701/yuanma/AndroidCamera(jb51.net).rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
微信轉賬輸入框規則(可能不全)1、小數點後兩位2、起始輸入小數點,顯示0.3、刪除到第一個位置是小數點的時候,第一個位置為0 ,避免出現小數點在第一個位置的情況修改這個朋
如下58同城快捷方式的效果: /** * 啟動某個activity是需要在manifest裡面定義 */ private void addShortCut
這裡記錄一下Canvas 相關API的使用,權當自己作筆記,以後需要好參考前面有一文Android應用程序窗口View的draw過程講到View的繪制過程,其中說到,Vi
Android系統支持的顏色是由4個值組成的,前3個為RGB,也就是我們常說的三原色(紅、綠、藍),最後一個值是A,也就是Alpha。這4個值都在0~255之間。顏色值越