欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android如何调用系统相机拍照

程序员文章站 2023-12-19 14:27:28
本文实例为大家分享了android调用系统相机拍照的具体代码,供大家参考,具体内容如下 /** * 调用系统相机 */ private vo...

本文实例为大家分享了android调用系统相机拍照的具体代码,供大家参考,具体内容如下

/**
   * 调用系统相机
   */
  private void takephoto() {
    uri uri = null;
    if (which_image == front_image) {
      frontfile = new file(getsdpath() +"/test/front_" + getdate() + ".jpg");
      uri = uri.fromfile(frontfile);
    } else if (which_image == back_image) {
      backfile = new file(getsdpath() + "/test/back_" + getdate() + ".jpg");
      uri = uri.fromfile(backfile);
    }
    intent captureintent = new intent(mediastore.action_image_capture);
    captureintent.putextra(mediastore.images.media.orientation, 0);
    captureintent.putextra(mediastore.extra_output, uri);
    captureintent.putextra("return-data", true);
    startactivityforresult(captureintent, take_photo);
  }

  /**
   * 获取系统时间
   *
   * @return
   */
  public static string getdate() {
    calendar ca = calendar.getinstance();
    int year = ca.get(calendar.year);      // 获取年份
    int month = ca.get(calendar.month);     // 获取月份
    int day = ca.get(calendar.date);      // 获取日
    int minute = ca.get(calendar.minute);    // 分
    int hour = ca.get(calendar.hour);      // 小时
    int second = ca.get(calendar.second);    // 秒

    string date = "" + year + (month + 1) + day + hour + minute + second;

    return date;
  }

  /**
   * 获取sd path
   *
   * @return
   */
  public string getsdpath() {
    file sddir = null;
    boolean sdcardexist = environment.getexternalstoragestate()
        .equals(android.os.environment.media_mounted); // 判断sd卡是否存在
    if (sdcardexist) {
      sddir = environment.getexternalstoragedirectory();// 获取跟目录
      return sddir.tostring();
    }

    return null;
  }

  @override
  protected void onactivityresult(int requestcode, int resultcode, intent data) {
    super.onactivityresult(requestcode, resultcode, data);
    if (requestcode == take_photo && resultcode == result_ok) {
//      bundle bundle = data.getextras();
//      bitmap bitmap = (bitmap) bundle.get("data");
//      if (which_image == front_image) {
//        ivcardfront.setimagebitmap(bitmap);
//      } else if (which_image == back_image) {
//        ivcardback.setimagebitmap(bitmap);
//      }
      try {
        if (which_image == front_image) {

          uri uri = uri.parse(mediastore.images.media.insertimage(getcontentresolver(), frontfile.getabsolutepath(), null, null));
          contentresolver contentresolver = this.getcontentresolver();
          bitmap bitmap = bitmapfactory.decodestream(contentresolver.openinputstream(uri));
          ivcardfront.setimagebitmap(bitmap);
        } else if (which_image == back_image) {
          uri uri = uri.parse(mediastore.images.media.insertimage(getcontentresolver(), backfile.getabsolutepath(), null, null));
          contentresolver contentresolver = this.getcontentresolver();
          bitmap bitmap = bitmapfactory.decodestream(contentresolver.openinputstream(uri));
          ivcardback.setimagebitmap(bitmap);
        }
      } catch (filenotfoundexception e) {
        e.printstacktrace();
      }
    }
  }

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

上一篇:

下一篇: