当前位置: 首页 > 工具软件 > TakePhoto > 使用案例 >

takephoto 框架_TakePhoto实现拍照得到图片和从相册得到图片

杨昆
2023-12-01

packagecom.example.car;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.content.SharedPreferences;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.net.Uri;importandroid.support.annotation.NonNull;importandroid.support.v7.app.AlertDialog;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.View;importandroid.widget.ImageView;importandroid.widget.Toast;importcom.jph.takephoto.app.TakePhoto;importcom.jph.takephoto.app.TakePhotoImpl;importcom.jph.takephoto.compress.CompressConfig;importcom.jph.takephoto.model.CropOptions;importcom.jph.takephoto.model.InvokeParam;importcom.jph.takephoto.model.TContextWrap;importcom.jph.takephoto.model.TResult;importcom.jph.takephoto.permission.InvokeListener;importcom.jph.takephoto.permission.PermissionManager;importcom.jph.takephoto.permission.TakePhotoInvocationHandler;importjava.io.File;//实现TakePhoto.TakeResultListener, InvokeListener接口

public class Setting extends AppCompatActivity implementsTakePhoto.TakeResultListener, InvokeListener {

TakePhoto takePhoto;

InvokeParam invokeParam;

String imagePath;

File file;

Uri uri;intsize;

CropOptions cropOptions;

ImageView headSculpture;

Bitmap bitmap;

File test;

SharedPreferences sharedPreferences;

SharedPreferences.Editor editor;

@Overrideprotected voidonCreate(Bundle savedInstanceState) {

getTakePhoto().onCreate(savedInstanceState);super.onCreate(savedInstanceState);

setContentView(R.layout.activity_setting);

initEvents();

addListener();

}public voidinitEvents(){//各控件初始化

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 voidaddListener(){

headSculpture.setOnClickListener(newView.OnClickListener() {

@Overridepublic voidonClick(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,newDialogInterface.OnClickListener() {

@Overridepublic void onClick(DialogInterface dialog, intwhich) {switch(which){case 0://拍照并裁剪

takePhoto.onPickFromCaptureWithCrop(uri, cropOptions);break;case 1://从照片选择并裁剪

takePhoto.onPickFromGalleryWithCrop(uri, cropOptions);break;default:break;

}

}

});

builder.show();

}

});

}

@Overrideprotected void onActivityResult(int requestCode, intresultCode, Intent data) {

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

}

@Overrideprotected voidonSaveInstanceState(Bundle outState) {

getTakePhoto().onSaveInstanceState(outState);super.onSaveInstanceState(outState);

}

@Overridepublic 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);

}

@OverridepublicPermissionManager.TPermissionType invoke(InvokeParam invokeParam) {

PermissionManager.TPermissionType type= PermissionManager.checkPermission(TContextWrap.of(this), invokeParam.getMethod());if(PermissionManager.TPermissionType.WAIT.equals(type)) {this.invokeParam =invokeParam;

}returntype;

}publicTakePhoto 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);returntakePhoto;

}

@Overridepublic void takeSuccess(finalTResult result) {//成功取得照片

test = newFile(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);

}

}

@Overridepublic voidtakeFail(TResult result, String msg) {//取得失败

Toast.makeText(Setting.this,"设置失败",Toast.LENGTH_SHORT).show();

}

@Overridepublic voidtakeCancel() {//取消

}

}

 类似资料: