图片选取,压缩,裁剪等的一个工具使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shb2058/article/details/68946651

https://github.com/crazycodeboy/TakePhoto

详细参照github,下位一个demo

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;


import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ImageChooseActivity extends TakePhotoActivity {

    @BindView(R.id.title)
    TitleView title;
    @BindView(R.id.img_show)
    ImageView imgShow;
    @BindView(R.id.btn_commit)
    RectangleButton btnCommit;

    private Context context = ImageChooseActivity.this;
    private Uri outputFileUri;
    private String imageUrisLocal = "";
    private String imageHtpPath = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_choose);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        title.setTitle("选择图片");
        title.setmTxtTitleVisible(View.INVISIBLE);
        title.setmImgRightImg(R.drawable.btn_camera);

        btnCommit.setmTxtBiggestBtn("确定");

        Intent intent = getIntent();
        imageUrisLocal = intent.getStringExtra("imgUriLocal");
        imageHtpPath = intent.getStringExtra("FeatureImgPath");

        if(!imageUrisLocal.equals("")) {
            Uri uri = Uri.parse(imageUrisLocal);
            imgShow.setImageURI(uri);
        } else if (!"".equals(imageHtpPath)) {
            ImageUtil.setImage(imageHtpPath, imgShow);
        }

        btnListener();
    }

    private void btnListener() {
        title.setBackListener(new TitleView.BackClickListener() {
            @Override
            public void backClick(View v, int pos) {
                if (pos == 0) {
                    finish();
                }

                if (pos == 1) {
                    showDialog();

                }
            }
        });

        btnCommit.setmBiggestBtn(new MyBtnClickListener() {
            @Override
            public void clickIt(View v) {
                Intent intent = new Intent(context, CreateEventBIActivity.class);
                intent.putExtra("imgUri", imageUrisLocal);
                setResult(201, intent);
                finish();
            }
        });
    }

    private void showDialog() {
        View bottomView = LayoutInflater.from(context).inflate(R.layout.layout_camera_gallery_dialog, null);
        TextView btnCamera = (TextView) bottomView.findViewById(R.id.btn_camera);
        TextView btnGallery = (TextView) bottomView.findViewById(R.id.btn_gallery);

        final MyDialog bottomDialog = new MyDialog(context, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.CENTER, bottomView, R.style.dialog);
        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                File file = FileUtils.createImageFile();
                outputFileUri = Uri.fromFile(file);
//去拍照,然后存储的地址
//设置压缩
CompressConfig compressConfig=new CompressConfig.Builder().setMaxSize(50*1024).setMaxPixel(800).create();
getTakePhoto().onEnableCompress(compressConfig,true);
getTakePhoto().onPickFromCapture(outputFileUri);

//设置裁剪
//                CropOptions cropOptions=new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(true).create();
//                getTakePhoto().onPickFromCaptureWithCrop(outputFileUri,cropOptions);

//                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//                intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
//                if (intent.resolveActivity(getPackageManager()) != null) {
//                    startActivityForResult(intent, 101);
//                }

                bottomDialog.dismiss();
            }
        });

        btnGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = FileUtils.createImageFile();
                outputFileUri = Uri.fromFile(file);
              getTakePhoto().onPickFromDocuments();
//                Intent intent = new Intent(Intent.ACTION_PICK);
//                intent.setType("image/*");
//                startActivityForResult(intent, 102);
//这里是进行裁剪的时候用的
//                CropOptions cropOptions=new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(true).create();
//                getTakePhoto().onPickFromDocumentsWithCrop(outputFileUri,cropOptions);
                bottomDialog.dismiss();
            }
        });
    }
    //重写tackphoto的方法,显示选择图片后的会掉


    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(TResult result) {
        Log.e("======","选取成功的回调"+"file://"+result.getImage().getCompressPath());
        //file://getOriginalPath-获取到原始的地址getcomp..---获取到压缩后地址
        imgShow.setImageURI(Uri.parse("file://"+result.getImage().getOriginalPath()));
//        Uri uri = Uri.parse("https://pic4.zhimg.com/03b2d57be62b30f158f48f388c8f3f33_b.png");
//        SimpleDraweeView commonImageView = (SimpleDraweeView) findViewById(R.id.commonImageView);
//        commonImageView.setImageURI(uri);
        //imgShow.setImageBitmap(result.getImage().getCompressPath());
        super.takeSuccess(result);
    }
}


阅读更多
换一批

没有更多推荐了,返回首页