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

android自定义Camera拍照并查看图片,第1张

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

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

1、打开相机

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

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:orIEntation="vertical" >  <SurfaceVIEw    androID:ID="@+ID/sv"    androID:layout_wIDth="match_parent"    androID:layout_height="0dp"    androID:layout_weight="1" />  <button    androID:ID="@+ID/btn_camera"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:text="拍照" /></linearLayout>

2、获取相机并开启预览

/**   * 获取系统相机   *    * @return   */  private Camera getcCamera() {    Camera camera = null;    try {      camera = Camera.open();    } catch (Exception e) {      camera = null;    }    return camera;  }
/**   * 显示相机预览图片   *    * @return   */  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);      }    });  }   /* 根据需求的宽和高以及图片实际的宽和高计算SampleSize   *       * @param options   * @param wIDth   * @param height   * @return       */  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 heigh@R_403_6205@io = Math.round(height * 1.0f / reqHeight);      inSampleSize = Math.max(wIDthRadio,heigh@R_403_6205@io);    }    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;  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的android自定义Camera拍照并查看图片全部内容,希望文章能够帮你解决android自定义Camera拍照并查看图片所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1145916.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存