文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android相机Camera基础知识

2022-06-06 04:38

关注

一、概述
本章节主要讲述的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);
  }
  
  @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;

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);;
      }
    });
  }
  
  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);
        }
      }
    });
  }
  
  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;

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和相册功能详解Android camera实时预览 实时处理,人脸识别示例Android自定义照相机Camera出现黑屏的解决方法Android利用Camera实现中轴3D卡牌翻转效果Android编程中调用Camera时预览画面有旋转问题的解决方法Android Camera是否支持变焦的判断方法总结Android Camera变焦编程步骤Android camera2 判断相机功能是否可控的实例


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯