栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

android自定义Camera拍照并查看图片

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android自定义Camera拍照并查看图片

本文实例为大家分享了android自定义Camera拍照并查看图片的具体代码,供大家参考,具体内容如下

1、打开相机

a.预览拍摄图片,需用到SurfaceView,并且实现其回调函数implements SurfaceHolder.Callback;
activity_camera.xml




  

  

2、获取相机并开启预览


  private Camera getcCamera() {
    Camera camera = null;
    try {
      camera = Camera.open();
    } catch (Exception e) {
      camera = null;
    }
    return camera;
  }


  private void showCameraView(Camera camera,SurfaceHolder holder)
  {
    try {
      camera.setPreviewDisplay(holder);
      camera.setDisplayOrientation(90);
      camera.startPreview();
    }catch (Exception e){
      e.printStackTrace();
    };

  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    showCameraView(mCamera, holder);
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    mCamera.stopPreview();
    showCameraView(mCamera, holder);
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    clearCamera();
  }
}

3、相机主页面处理

public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback{

  private SurfaceView sv;
  private Camera mCamera;
  private SurfaceHolder holder;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
    sv = (SurfaceView)findViewById(R.id.sv);
    holder = sv.getHolder();
    holder.addCallback(this);

    findViewById(R.id.btn_camera).setonClickListener(new View.onClickListener() {
      @Override
      public void onClick(View v) {
 // 获取当前相机参数
 Camera.Parameters parameters = mCamera.getParameters();
 // 设置相片格式
 parameters.setPictureFormat(ImageFormat.JPEG);
 // 设置预览大小
 parameters.setPreviewSize(800, 480);
 // 设置对焦方式,这里设置自动对焦
 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, new Camera.PictureCallback() {
  @Override
  public void onPictureTaken(byte[] data, Camera camera) {
    // data为完整数据
    File file = new File("/sdcard/photo.png");
    // 使用流进行读写
    try {
      FileOutputStream fos = new FileOutputStream(file);
      try {
 fos.write(data);
 // 关闭流
 fos.close();
 // 查看图片
 Intent intent = new Intent();
 // 传递路径
 intent.putExtra("path", file.getAbsolutePath());
 setResult(0,intent);
 finish();
      } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
      }
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
});
     }
   }
 });
      }
    });

  }

 @Override
  protected void onResume() {
    super.onResume();
    if (mCamera == null) {
      mCamera = getCamera();
      if (holder != null) {
 showCameraView(mCamera, holder);
      }
    }
  }
  @Override
  protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    // activity暂停时我们释放相机内存
    clearCamera();

  }
  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

  
  private void clearCamera() {

    // 释放hold资源
    if (mCamera != null) {
      // 停止预览
      mCamera.stopPreview();
      mCamera.setPreviewCallback(null);
      // 释放相机资源
      mCamera.release();
      mCamera = null;
    }
  }

4、启动activity(设置拍照完图片路径返回显示图片处理,一定要对图片进行采样率操作(很可能拍照的图片太多而无法显示,又不报任何异常))

public class MainActivity extends AppCompatActivity {

  private ImageView cameraIv;

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == 0 && requestCode == 100)
    {
      String path = data.getStringExtra("path");
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(path, options);
      ImageSize imageSize = getImageViewSize(cameraIv);
      options.inSampleSize = caculateInSampleSize(options,
   imageSize.width, imageSize.height);

      // 使用获得到的InSampleSize再次解析图片
      options.inJustDecodeBounds = false;
      Bitmap bitmap = BitmapFactory.decodeFile(path, options);
      cameraIv.setImageBitmap(bitmap);
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button openButton = (Button) findViewById(R.id.button);
    cameraIv = (ImageView) findViewById(R.id.imageView);



    openButton.setonClickListener(new View.onClickListener() {
      @Override
      public void onClick(View v) {
 Intent intent = new Intent(MainActivity.this,CameraActivity.class);
 startActivityForResult(intent,100);
      }
    });
  }

   
  public static int caculateInSampleSize(BitmapFactory.Options options, int reqWidth,
 int reqHeight)
  {
    int width = options.outWidth;
    int height = options.outHeight;

    int inSampleSize = 1;

    if (width > reqWidth || height > reqHeight)
    {
      int widthRadio = Math.round(width * 1.0f / reqWidth);
      int heightRadio = Math.round(height * 1.0f / reqHeight);

      inSampleSize = Math.max(widthRadio, heightRadio);
    }

    return inSampleSize;
  }

  public static ImageSize getImageViewSize(ImageView imageView)
  {

    ImageSize imageSize = new ImageSize();
    DisplayMetrics displayMetrics = imageView.getContext().getResources()
 .getDisplayMetrics();

    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)imageView.getLayoutParams();

    int width = imageView.getWidth();// 获取imageview的实际宽度
    if (width <= 0)
    {
      width = lp.width;// 获取imageview在layout中声明的宽度
    }

    if (width <= 0)
    {
      width = displayMetrics.widthPixels;
    }

    int height = imageView.getHeight();// 获取imageview的实际高度
    if (height <= 0)
    {
      height = lp.height;// 获取imageview在layout中声明的宽度
    }

    if (height <= 0)
    {
      height = displayMetrics.heightPixels;
    }
    imageSize.width = width;
    imageSize.height = height;

    return imageSize;
  }

  public static class ImageSize
  {
    int width;
    int height;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158298.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号