Android相机Camera基础知识
一、概述
本章节主要讲述的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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
分享标题:Android相机Camera基础知识
网站链接:http://scyingshan.cn/article/gdejhi.html