Android应用开发之Camera解析
凌雪 2018-09-21 来源 :网络 阅读 535 评论 0

摘要:本文将带你了解Android应用开发之Camera解析,希望本文对大家学Android有所帮助。

本文将带你了解Android应用开发之Camera解析,希望本文对大家学Android有所帮助。


Android开发之Camera解析
   
   
    AndroidManifest.xml
    <!--?xml version="1.0"   encoding="utf-8"?--><manifest   package="com.hala.camera"   xmlns:android="https://schemas.android.com/apk/res/android"> <uses-permission   android:name="android.permission.WRITE_EXTERNAL_STORAGE"> <uses-permission   android:name="android.permission.CAMERA">      <intent-filter>    <category   android:name="android.intent.category.LAUNCHER"></category></action></intent-filter>  </activity> </application> </uses-permission></uses-permission></manifest>
    activity_main.xml
    ?12345678<!--?xml version="1.0"   encoding="utf-8"?--><linearlayout   android:layout_height="match_parent"   android:layout_width="match_parent"   android:orientation="vertical" tools:context="com.hala.camera.MainActivity"   xmlns:android="https://schemas.android.com/apk/res/android"   xmlns:app="https://schemas.android.com/apk/res-auto"   xmlns:tools="https://schemas.android.com/tools"><button   android:id="@+id/button"   android:layout_height="wrap_content"   android:layout_width="match_parent"   android:onclick="systemCamera" android:text="System   Camera"></button><button android:id="@+id/button4"   android:layout_height="wrap_content"   android:layout_width="match_parent"   android:onclick="cameraAPI" android:text="Camera   API"></button><button android:id="@+id/button5"   android:layout_height="wrap_content"   android:layout_width="match_parent"   android:onclick="takePhoto" android:text="take   photo">  <framelayout   android:id="@+id/cameraLayout"   android:layout_gravity="center_horizontal"   android:layout_height="300dp"   android:layout_width="300dp">  <imageview   android:id="@+id/imageView"   android:layout_height="wrap_content"   android:layout_width="match_parent"   app:srccompat="@mipmap/ic_launcher"> </imageview></framelayout></button></linearlayout>
    MainActivty.java
      ?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205package   com.hala.camera; import android.content.Context;import   android.content.Intent;import android.hardware.Camera;import   android.net.Uri;import android.os.Environment;import   android.provider.MediaStore;import   android.support.v7.app.AppCompatActivity;import android.os.Bundle;import   android.view.SurfaceHolder;import android.view.SurfaceView;import   android.view.View;import android.widget.FrameLayout;import   android.widget.ImageView; import java.io.File;import   java.io.FileNotFoundException;import java.io.FileOutputStream;import   java.io.IOException; public class MainActivity extends AppCompatActivity   { public static final String FILE_PATH =   Environment.getExternalStorageDirectory().getPath() +   "/imooc/temp.jpg"; public static final int REQUEST_CODE =   0; private ImageView mImageView; private FrameLayout   mCameraLayout; private Camera mCamera; private CameraView   mCameraView;  @Override protected void onCreate(Bundle   savedInstanceState)   {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);   mImageView   = (ImageView) findViewById(R.id.imageView);  mCameraLayout =   (FrameLayout) findViewById(R.id.cameraLayout); }  public void   systemCamera(View view) {   if (mCameraView != null) {mCameraView.release();  }  //   open system camera.   Intent intent = new   Intent();  intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);  intent.addCategory(Intent.CATEGORY_DEFAULT);   File   file = new File(FILE_PATH);  if (file.exists())   {file.delete();  }   // 文件转换成uri  Uri uri =   Uri.fromFile(file);  intent.putExtra(MediaStore.EXTRA_OUTPUT,   uri);   startActivityForResult(intent,   REQUEST_CODE); }  @Override protected void   onActivityResult(int requestCode, int resultCode, Intent data)   {  super.onActivityResult(requestCode, resultCode,   data);  if (requestCode == REQUEST_CODE) {File file = new   File(FILE_PATH);if (file.exists())   { mImageView.setImageURI(Uri.fromFile(file));}  } }  public   void cameraAPI(View view) {  // 创建一个camera视图。  //   将camera视图添加到cameraLayout里。  // 做一些camera的设置  if (mCamera == null && mCameraView == null)   {mCamera = getCamera();mCameraView = new CameraView(this,   mCamera);mCameraLayout.addView(mCameraView); Camera.Parameters   parameters = mCamera.getParameters();parameters.setRotation(90);mCamera.setParameters(parameters);  } }  public   void takePhoto(View view) {  // 点击按钮拍照,并保存到存储里。  if (mCamera != null) {try   { mCamera.takePicture(null, null, new Camera.PictureCallback()   {  @Override  public void onPictureTaken(byte[] data,   Camera camera) {File file = new File(FILE_PATH);try { FileOutputStream   fileOutputStream = new   FileOutputStream(file); fileOutputStream.write(data); fileOutputStream.close();}   catch (FileNotFoundException e) { e.printStackTrace();} catch   (IOException e) { e.printStackTrace();}  } });} catch   (Exception e)   { e.printStackTrace();}  } }  public Camera   getCamera() {  Camera camera = null;  try {camera =   Camera.open();  } catch (Exception e) {e.printStackTrace();// 错误提示  }  return   camera; }  public class CameraView extends SurfaceView   implements SurfaceHolder.Callback {   Camera   mCamera;  private final SurfaceHolder   mSurfaceHolder;   public CameraView(Context context, Camera   camera) {super(context);mCamera = camera;mSurfaceHolder =   getHolder();mSurfaceHolder.addCallback(this);  }   @Override  public   void surfaceCreated(SurfaceHolder holder) {try { if (mCamera != null)   {  mCamera.setPreviewDisplay(holder);  mCamera.setDisplayOrientation(90);  mCamera.startPreview(); }}   catch (IOException e)   { e.printStackTrace();}  }   @Override  public   void surfaceChanged(SurfaceHolder holder, int format, int width, int height)   {if (mSurfaceHolder.getSurface() == null) { return;} if (mCamera !=   null) { mCamera.stopPreview();} try { if (mCamera != null)   {  mCamera.setPreviewDisplay(holder);  mCamera.startPreview(); }}   catch (IOException e)   { e.printStackTrace();}   }   @Override  public   void surfaceDestroyed(SurfaceHolder holder) {release();  }   public   void release() {if (mCamera != null)   { mCamera.setPreviewCallback(null); mCamera.stopPreview(); mCamera.release(); mCamera   = null;}  } }  @Override protected void   onPause() {  super.onPause();  if (mCameraView != null)   {mCameraView.release();  } }  @Override protected   void onResume() {  super.onResume();  mCamera =   getCamera();  mCameraLayout.removeAllViews();  mCameraView   = new CameraView(this,   mCamera);  mCameraLayout.addView(mCameraView);   Camera.Parameters   params =   mCamera.getParameters();  params.setRotation(90);  mCamera.setParameters(params); }}    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注移动开发之Android频道!

本文由 @凌雪 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程