拍照与选取相册图片-cameralibrary

末鹿安然 提交于 2020-02-07 08:01:46

版权声明:本文为延成原创文章,转载请标明出处

cameralibrary

github地址:https://github.com/enChenging/cameraAndPhotos

效果图如下:

用法

Android Studio

将其添加到存储库build.gradle中

allprojects {
    repositories {
      	...
        maven{url 'https://jitpack.io'}
    }
}

在build.gradle文件中的dependencies下添加引用:

implementation 'com.github.enChenging:cameraAndPhotos:1.0'

详细使用见工程里的simple

使用核心代码:

Bimp.selectBitmap.clear();// 清空图册
Bimp.max = 3;// 初始化最大选择数
Bimp.themeColor = R.color.colorPrimary;//设置图册主题风格

    gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
adapter = new GridAdapter(this);
    adapter.update();
    gridview.setAdapter(adapter);
    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        if (arg2 == Bimp.selectBitmap.size()) {
            Type = 3;
            mAlert3.show();
        } else {
            Intent intent = new Intent(MainActivity.this, PhotoActivity.class);
            intent.putExtra("ID", arg2);
            startActivity(intent);
        }
    }
});

mAlert = new Alert(MainActivity.this)
            .builder(Alert.Type.BOTTOM)
            .addItem("拍照")
            .addItem("图片")
            .setOnItemClickListener(new Alert.OnAlertItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        if (position == 0) {
            Utils.camera(MainActivity.this);
        } else {
            Utils.photo(MainActivity.this);
        }
    }
});
mAlert2 = new Alert(MainActivity.this)
            .builder(Alert.Type.BOTTOM)
            .addItem("拍照(裁剪)")
            .addItem("图片(裁剪)")
            .setOnItemClickListener(new Alert.OnAlertItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        if (position == 0) {
            Utils.camera(MainActivity.this);
        } else {
            Utils.photo(MainActivity.this);
        }
    }
});
mAlert3 = new Alert(MainActivity.this)
            .builder(Alert.Type.BOTTOM)
            .addItem("拍照")
            .addItem("图片(选取多张图片)")
            .setOnItemClickListener(new Alert.OnAlertItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        if (position == 0) {
            Utils.camera(MainActivity.this);
        } else {
            startActivity(new Intent(MainActivity.this, ImageGridActivity.class));
            overridePendingTransition(R.anim.activity_translate_in, R.anim.activity_translate_out);
        }
    }
});

@SuppressLint("StringFormatInvalid")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {
        case Utils.CAMERA_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                switch (Type) {
                    case 1:
                        //压缩图片
                        Bitmap bitmap = Utils.decodeFile(Utils.mTempFile.getAbsolutePath());
                        mIv_image.setImageBitmap(bitmap);
                        File picFile = Utils.setPicToSdCard(PATH_SD_ICON, bitmap);
                        uploadPicTask(picFile);
                        break;
                    case 2:
                        //裁剪图片
                        Uri imageContentUri = Utils.getImageContentUri(MainActivity.this, Utils.mTempFile);
                        Utils.cropPhoto(this, imageContentUri);
                        break;
                    case 3:
                        ImageItem takePhoto = new ImageItem();
                        takePhoto.setImagePath(Utils.mTempFile.getPath());
                        Bimp.selectBitmap.add(takePhoto);
                        break;

                }
            }
            break;
        case Utils.PHOTO_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                switch (Type) {
                    case 1: {
                        //压缩图片
                        Bitmap bitmap = Utils.decodeUri(this, data.getData());
                        mIv_image.setImageBitmap(bitmap);
                        break;
                    }
                    case 2: {
                        //裁剪图片
                        Utils.cropPhoto(this, data.getData());
                        break;
                    }
                }
            }
            break;
        case Utils.CROP_PHOTO_REUQEST_CODE:
            if (resultCode == RESULT_OK) {
                try {
                    mBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(Utils.mUritempFile));
                    mIv_image.setImageBitmap(mBitmap);
                    File picFile = Utils.setPicToSdCard(PATH_SD_ICON, mBitmap);
                    uploadPicTask(picFile);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

混淆

#cameralibrary
-dontwarn com.release.cameralibrary.**
-keep class com.release.cameralibrary.**{*;}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!