android拍照上传

我们需要导包:app:implementation 'com.jph.takephoto:takephoto_library:4.0.3'

布局

<?xml version="1.0" encoding="UTF-8"?>

    <LinearLayout
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">


    -<LinearLayout
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/ll_button">

    <Button
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:id="@+id/btn_take_photo2"
        android:text="拍照"
        android:layout_weight="1"
        android:layout_marginLeft="30dp"/>

    <Button
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:id="@+id/btn_choose_photo2"
        android:text="相册"
        android:layout_weight="1"
        android:layout_marginLeft="30dp"/>

</LinearLayout>

    <ImageView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/iv_image"
        android:background="@mipmap/logo_bg"
        android:layout_centerHorizontal="true"/>

</LinearLayout>

activity的实现

public class PhotoActivity extends TakePhotoActivity {
    private Button btn_take_photo2, btn_choose_photo2;
    ImageView imageView = null;

    TakePhoto takePhoto;
    File file;
    Uri imageUri;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);

        btn_take_photo2 = (Button) findViewById(R.id.btn_take_photo2);
        btn_choose_photo2 = (Button) findViewById(R.id.btn_choose_photo2);
        imageView = (ImageView) findViewById(R.id.iv_image);

        initView();
        //从相册中选图片
        btn_choose_photo2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CropOptions cropOptions = new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(true).create();
                takePhoto.onPickFromDocumentsWithCrop(imageUri, cropOptions);
            }
        });
        //拍照
        btn_take_photo2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CropOptions cropOptions = new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(true).create();
                takePhoto.onPickFromCaptureWithCrop(imageUri,cropOptions);
            }
        });
    }

    public void initView() {
        takePhoto = getTakePhoto();
        CropOptions.Builder builder = new CropOptions.Builder();
        builder.setAspectX(800).setAspectY(800);
        builder.setWithOwnCrop(true);
        file = new File(Environment.getExternalStorageDirectory(),
                "/temp/" + System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists()) {
            boolean mkdirs = file.getParentFile().mkdirs();
            if (!mkdirs) {
                Toast.makeText(this, "文件目录创建失败", Toast.LENGTH_SHORT).show();
            }
        }
        imageUri = Uri.fromFile(file);
        CompressConfig config = new CompressConfig.Builder()
                .setMaxSize(102400)
                .setMaxPixel(400)
                .enableReserveRaw(true)
                .create();
        takePhoto.onEnableCompress(config, true);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        Log.e("111",result.getImage().getCompressPath());
        //这是压缩后的路径
        Glide.with(this).load(result.getImage().getCompressPath()).into(imageView);
        //这是未压缩的路径
//        Glide.with(this).load(result.getImage().getOriginalPath()).into(imageView);

    }

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

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }
}
源码地址下载:https://download.csdn.net/download/weixin_42267745/10432680


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值