TakePhoto实现拍照得到图片和从相册得到图片

时间:2021-11-22 21:09:30

在学郭霖大神的第一行代码的时候,学到利用相机拍照和从本地相册取照片的那一小节的时候,代码写出来但是出了很多问题,APP老是崩溃,一番百度最终还是没有找到解决办法

无奈只能用别人现成的*了,然后就发现了这个TakePhoto框架,很多人都推荐用这个,所以写下这个博客来记录一下这个框架的基本使用

TakePhoto是一个很优秀的关于图片的一些处理的第三方框架

GitHub地址如下:https://github.com/crazycodeboy/TakePhoto

配置的办法很简单,在工程目录的APP目录下的build.gradle里面的dependencies标签加入依赖即可

TakePhoto实现拍照得到图片和从相册得到图片

compile 'com.jph.takephoto:takephoto_library:4.0.3'

官方的最新版本是4.1.0,但我这边加入4.1.0的依赖会报错,然后我试了一下4.0.3的是可以的,所以就直接用4.0.3了

大致实现过程就是实现TakePhoto.TakeResultListener, InvokeListener接口

然后重写几个方法就行,方法体里面的内容根据自己的项目需要进行编写

package com.example.car;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast; import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.InvokeParam;
import com.jph.takephoto.model.TContextWrap;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.permission.InvokeListener;
import com.jph.takephoto.permission.PermissionManager;
import com.jph.takephoto.permission.TakePhotoInvocationHandler; import java.io.File; //实现TakePhoto.TakeResultListener, InvokeListener接口
public class Setting extends AppCompatActivity implements TakePhoto.TakeResultListener, InvokeListener {
TakePhoto takePhoto;
InvokeParam invokeParam;
String imagePath;
File file;
Uri uri;
int size;
CropOptions cropOptions;
ImageView headSculpture;
Bitmap bitmap;
File test;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
getTakePhoto().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
initEvents();
addListener();
}
public void initEvents(){
//各控件初始化
file = new File(getExternalCacheDir(), System.currentTimeMillis() + ".png");
uri = Uri.fromFile(file);
size = Math.min(getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
cropOptions = new CropOptions.Builder().setOutputX(size).setOutputX(size).setWithOwnCrop(false).create();
headSculpture = (ImageView)findViewById(R.id.head_sculpture);
sharedPreferences = getSharedPreferences("Setting",MODE_MULTI_PROCESS);
imagePath = sharedPreferences.getString("image_path","");
Log.d("Image_path的值是",imagePath+".....");
if(!imagePath.equals("")){
headSculpture.setImageBitmap(BitmapFactory.decodeFile(imagePath));
} }
public void addListener(){
headSculpture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//弹出框框
AlertDialog.Builder builder = new AlertDialog.Builder(Setting.this,android.R.style.Theme_Holo_Light_Dialog);
builder.setIcon(R.drawable.ic_choice_pic);
builder.setTitle("选择");
String[] choices = {"拍照","从相机里选择"};
builder.setItems(choices, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
//拍照并裁剪
takePhoto.onPickFromCaptureWithCrop(uri, cropOptions);
break;
case 1:
//从照片选择并裁剪
takePhoto.onPickFromGalleryWithCrop(uri, cropOptions);
break;
default:
break;
}
}
});
builder.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
getTakePhoto().onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
} @Override
protected void onSaveInstanceState(Bundle outState) {
getTakePhoto().onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
} @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//以下代码为处理Android6.0、7.0动态权限所需
PermissionManager.TPermissionType type = PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
PermissionManager.handlePermissionsResult(this, type, invokeParam, this);
} @Override
public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
PermissionManager.TPermissionType type = PermissionManager.checkPermission(TContextWrap.of(this), invokeParam.getMethod());
if (PermissionManager.TPermissionType.WAIT.equals(type)) {
this.invokeParam = invokeParam;
}
return type;
} public TakePhoto getTakePhoto() {
//获得TakePhoto实例
if (takePhoto == null) {
takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
}
//设置压缩规则,最大500kb
takePhoto.onEnableCompress(new CompressConfig.Builder().setMaxSize(500 * 1024).create(), true);
return takePhoto;
} @Override
public void takeSuccess(final TResult result) {
//成功取得照片
test = new File(result.getImage().getOriginalPath());
editor = sharedPreferences.edit();
editor.putString("image_path",result.getImage().getOriginalPath());
editor.commit();
if(test.exists()){
bitmap = BitmapFactory.decodeFile(result.getImage().getOriginalPath());
headSculpture.setImageBitmap(bitmap);
}
} @Override
public void takeFail(TResult result, String msg) {
//取得失败
Toast.makeText(Setting.this,"设置失败",Toast.LENGTH_SHORT).show();
} @Override
public void takeCancel() {
//取消
} }

取出照片还有一些其他操作可能需要看一下里面TResult和TImage这两个类

效果如下

TakePhoto实现拍照得到图片和从相册得到图片