当前位置: 移动技术网 > 移动技术>移动开发>Android > android系统拍照结合android-crop裁剪图片

android系统拍照结合android-crop裁剪图片

2019年07月24日  | 移动技术网移动技术  | 我要评论

在一个应用中更换用户的头像,一般有拍照和从图库中选择照片两种方法,现在网上也有很多开源的,但是很多都太复杂。而 android-crop 这个库比较小,代码不复杂,比较适合,但是它没有拍照这个功能,需要我们自己整合进去。

调用系统相机拍照

1、返回略缩图的拍照

 // 调用系统的拍照
  private void dispatchtakepictureintent() {
    intent takepictureintent = new intent(mediastore.action_image_capture);
    if (takepictureintent.resolveactivity(getpackagemanager()) != null) {
      startactivityforresult(takepictureintent, request_image_capture);
    }
  }

重写 onactivityresult 方法,系统相机会返回略缩图

@override
protected void onactivityresult(int requestcode, int resultcode, intent data) {
  if (requestcode == request_image_capture && resultcode == result_ok) {
    bundle extras = data.getextras();
    bitmap imagebitmap = (bitmap) extras.get("data");
    mimageview.setimagebitmap(imagebitmap);
  }
}

1.返回全图片
. 如果是全图片,一般都是存在手机的外部存储空间,这个时候需要读写外部的权限

<uses-permission android:name="android.permission.write_external_storage"
           android:maxsdkversion="18"/>
<uses-permission android:name="android.permission.read_external_storage"/>

在4.4 及其以上不需要 wrte_external_storage 权限,所以,我们添加了 android: maxsdkversion = “18”

. 因为是全图片,图片一般都会很大,我们不能再像上面一样全部数据返回,不然很容易就出现 oom , 所以,启动拍照的时候设置一个指定路径保存图片,拍照成功之后就可以直接使用这个路径了

// 图片路径
 private uri mcurrentphotouri;

// 拍照
private void dispatchtakepictureintent() {
  intent takepictureintent = new intent(mediastore.action_image_capture);
  if (takepictureintent.resolveactivity(mcontext.getpackagemanager()) != null) {
      file photofile = null;
      try {
        photofile = createimagefile();
      } catch (ioexception e) {
        e.printstacktrace();
      }

      if (photofile != null){
        uri photouri = fileprovider.geturiforfile(mcontext, "com.yxhuang.fileprovider", photofile);
        takepictureintent.putextra(mediastore.extra_output, photouri);

        mcurrentphotouri = photouri;

        startactivityforresult(takepictureintent, request_image_capture);
      }
    }
  }

 // 创建图片路径
 private file createimagefile() throws ioexception {
  string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date());
    string imagefilename = "jpeg_" + timestamp + "_";
    file storagedir = mcontext.getexternalfilesdir(environment.directory_pictures);
    file image = file.createtempfile(
        imagefilename,   /* prefix */
        ".jpg",       /* suffix */
        storagedir     /* directory */
    );

    return image;
  }

在创建图片 uri 的时候,使用了 fileprovider, fileprovider 只是 contentprovider 的一个子类。用于方便文件的共享。
fileprovider 需要一些配置
在 manifext.xml 中在节点下

<!--自定义uri 需要 provider -->
<provider
  android:name="android.support.v4.content.fileprovider"
      android:authorities="com.yxhuang.fileprovider"
      android:exported="false"
      android:granturipermissions="true">
      <meta-data
        android:name="android.support.file_provider_paths"
        android:resource="@xml/file_paths">
      </meta-data>
    </provider>

其中 authorities 的内容要与 fileprovider.geturiforfile()方法中的第二个参数相同。

同时也要对android:resource 进行配置
res/xml/file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-path name="image_file" 
      path="android/data/com.yxhuang/files/pictures"/>
</paths>

使用 android-crop 开源库裁剪图片

android-crop 是一个比较简单的图片裁剪库。具体使用看 github 的 demo. 我们这里结合拍照一起使用。
上面我们已经准备好拍照了,我们需要在 onactivityforresult() 方法中进行处理

 @override
  public void onactivityresult(int requestcode, int resultcode, intent data) {
    // 拍照返回
    if (resultcode == result_ok){
      if (requestcode == request_image_capture){
        begincrop(mcurrentphotouri);
      } else if (requestcode == crop.request_pick){
        begincrop(data.getdata());
      }
    }

    // 裁剪
    if (requestcode == crop.request_crop){
      handlecrop(resultcode, data);
    }
  }

// 开始裁剪
private void begincrop(uri source){
   uri destination = uri.fromfile(new file(mcontext.getcachedir(), "cropped"));
     // start() 方法根据其的需求选择不同的重载方法
    crop.of(source, destination).assquare().start(getactivity(), minefragment.this);
  }

// 将裁剪回来的数据进行处理
 private void handlecrop(int resultcode, intent result){
    if (resultcode == result_ok){
      mimageview.setimageuri(crop.getoutput(result));
    } else if (resultcode == crop.result_error){
      toast.maketext(mcontext, 
          crop.geterror(result).getmessage(), 
          toast.length_short).show();
    }
  }

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网