一、概述
本章節(jié)主要講述的Android相機(jī)Camera的相關(guān)知識(shí)點(diǎn),主要包含三個(gè)方面,Android啟動(dòng)系統(tǒng)相機(jī)生成縮略圖、Android啟動(dòng)系統(tǒng)相機(jī)生成原圖以及Android自定義相機(jī)等。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信平臺(tái)小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了富錦免費(fèi)建站歡迎大家使用!
二、Android啟動(dòng)系統(tǒng)相機(jī)
核心代碼:
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調(diào)用 Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(it, REQ_1); } public void startCamera2(View view){ //隱式Intent調(diào)用 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自定義相機(jī)
1.自定義相機(jī)步驟:
創(chuàng)建相機(jī)
創(chuàng)建SurfaceView
關(guān)聯(lián)相機(jī)與SurfaceView
調(diào)整相機(jī)顯示效果
自定義相機(jī)預(yù)覽界面
2.核心代碼:
自定義相機(jī)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); //點(diǎn)擊自動(dòng)對(duì)焦 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);//自動(dòng)對(duì)焦 mCamera.autoFocus(new Camera.AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { //對(duì)焦準(zhǔn)確拍攝照片 if(success){ mCamera.takePicture(null, null, mPictureCallback); } } }); } /** * 獲取相機(jī)對(duì)象 * @return */ private Camera getCamera(){ mCamera = Camera.open(); return mCamera; } /** * 開(kāi)始預(yù)覽相機(jī)內(nèi)容 */ private void setStartPreview(Camera camera, SurfaceHolder holder){ try { camera.setPreviewDisplay(holder); camera.setDisplayOrientation(90);//設(shè)置相機(jī)預(yù)覽為豎屏90度 camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } /** * 釋放相機(jī)資源 */ 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(); } }
生成圖片結(jié)果頁(yè)面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);//旋轉(zhuǎn)90度矩陣 bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); result_iv.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
四、項(xiàng)目源代碼下載
http://xiazai.jb51.net/201701/yuanma/AndroidCamera(jb51.net).rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。