手机拍照及裁剪图片

注:我是在上一篇博客:“手机拍照及简单的图片压缩”的基础上改的,所以,权限等,参考上一篇,这里只对核心代码做说明
图片被裁剪后,清晰度不够,这个问题我没解决
1、效果图:

这里写图片描述

这里写图片描述

2、代码:
2.1:全局变量:

/**
 * 用于保存全局变量值
 */
public class CHEN {

    /**
     * 裁剪后图片保存的地址
     */
    public static String crop_img_path = Environment.getExternalStorageDirectory() + "/chen/crop";

    /**
     * 拍照的返回请求码
     */
    public static int TakePhotoRequestCode = 123;

    /**
     * 裁剪的返回请求码
     */
    public static int CropImgRequestCode = 234;

}

2.2:布局:activity_main_22

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#F6F6F6"
              android:fitsSystemWindows="true"
              android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/search_gold_expert_back_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#ec434b"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="点击icon"
            android:textColor="#ffffff"
            android:textSize="20sp"/>

    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >

            <ImageView
                android:id="@+id/iv_image"
                android:layout_width="310dp"
                android:layout_height="250dp"
                android:layout_gravity="center_horizontal"
                android:layout_margin="10dp"
                android:src="@mipmap/ic_launcher"/>

            <TextView
                android:id="@+id/confirm_tv"
                android:layout_width="fill_parent"
                android:layout_height="38dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="#ec434b"
                android:gravity="center"
                android:onClick="onClick"
                android:text="提交"
                android:textColor="#4F4F4F"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:lineSpacingMultiplier="1.4"
                android:text="拍照得到照片的原路径"
                android:textColor="#4F4F4F"
                android:textSize="16sp"/>

            <TextView
                android:id="@+id/result_url_tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:lineSpacingMultiplier="1.4"
                android:textColor="#4F4F4F"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:lineSpacingMultiplier="1.4"
                android:text="拍照得到照片的大小"
                android:textColor="#4F4F4F"
                android:textSize="16sp"/>

            <TextView
                android:id="@+id/photo_src_size"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:lineSpacingMultiplier="1.4"
                android:textColor="#4F4F4F"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:lineSpacingMultiplier="1.4"
                android:text="裁剪后的图片的路径"
                android:textColor="#4F4F4F"
                android:textSize="16sp"/>

            <TextView
                android:id="@+id/crop_img_path"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:lineSpacingMultiplier="1.4"
                android:textColor="#4F4F4F"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:lineSpacingMultiplier="1.4"
                android:text="裁剪后照片的大小"
                android:textColor="#4F4F4F"
                android:textSize="16sp"/>

            <TextView
                android:id="@+id/crop_img_size"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:lineSpacingMultiplier="1.4"
                android:textColor="#4F4F4F"
                android:textSize="16sp"/>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

2.3:主代码:MainActivity_22_Photo_Crop_Img


/**
 * 拍照,并裁剪图片
 */
public class MainActivity_22_Photo_Crop_Img extends BaseActivity implements View.OnClickListener, PopSelectListener {

    ImageView iv_image;
    TextView confirm_tv;
    TextView result_url_tv;
    TextView photo_src_size;

    TextView crop_img_path;
    TextView crop_img_size;

    PhotoFromSelectPop photoFromSelectPop;

    String url;
    String imageName;

    @Override
    void initview() {
        setContentView(R.layout.activity_main_22);

        iv_image = (ImageView) findViewById(R.id.iv_image);
        confirm_tv = (TextView) findViewById(R.id.confirm_tv);
        result_url_tv = (TextView) findViewById(R.id.result_url_tv);
        photo_src_size = (TextView) findViewById(R.id.photo_src_size);

        crop_img_path = (TextView) findViewById(R.id.crop_img_path);
        crop_img_size = (TextView) findViewById(R.id.crop_img_size);

        iv_image.setOnClickListener(this);
        confirm_tv.setOnClickListener(this);

        File file = new File(CHEN.img_path);
        if (!file.exists()) {
            try {
                file.mkdirs();
                Toast.makeText(this, "路径创建成功", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(this, "路径创建失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_image:

                photoFromSelectPop = new PhotoFromSelectPop(this);
                photoFromSelectPop.initPopupWindow(this);

                break;
            case R.id.confirm_tv:

                break;
        }

    }

    @Override
    public void popSelect(int flag) {

        switch (flag) {
            case 0:

                Toast.makeText(this, "从相册选取", Toast.LENGTH_SHORT).show();

                break;

            case 1:
                //拍照
                imageName = "/" + System.currentTimeMillis() + ".jpg";
                Intent intent_photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent_photo.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(CHEN.img_path, imageName)));
                startActivityForResult(intent_photo, CHEN.TakePhotoRequestCode);
                break;

        }
    }

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

        Log.e("requestCode", requestCode + "");
        Log.e("resultCode", resultCode + "");

        if (CHEN.TakePhotoRequestCode == requestCode) {
            url = CHEN.img_path + imageName;
            if (!TextUtils.isEmpty(url)) {
                result_url_tv.setText(url);
                File file = new File(url);
                photo_src_size.setText("图片大小==KB==" + Utils.getNumAccuracy_2(file.length() / 1024f) + "---图片大小==MB==" + Utils.getNumAccuracy_2(file.length() / (1024f * 1024f)));
                cropImage(file);

            }
        }

        if (CHEN.CropImgRequestCode == requestCode) {
            try {
                if (data != null) {
                    Bitmap bitmap_crop = data.getParcelableExtra("data");
                    //裁剪的图片保存到本地
                    File cropedfile = saveCroppedImage(bitmap_crop);
                    crop_img_path.setText(cropedfile.getPath());
                    crop_img_size.setText("图片大小==KB==" + Utils.getNumAccuracy_2(cropedfile.length() / 1024f));
                } else {
                    Toast.makeText(this, "取消裁剪", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                e.printStackTrace();

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

    /**
     * 裁剪照片
     *
     * @param file
     */
    private void cropImage(File file) {
        Uri mUri = Uri.fromFile(file);
        if (null == mUri)
            return;
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(mUri, "image/*");// mUri是已经选择的图片Uri
        intent.putExtra("crop", "true");// crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        //宽高比例
        intent.putExtra("aspectX", 2);
        intent.putExtra("aspectY", 1.9);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("outputFormat", "JPEG");
        intent.putExtra("scale", true);
        intent.putExtra("noFaceDetection", true);
        //若为false则表示不返回数据
        intent.putExtra("return-data", true);
        startActivityForResult(intent, CHEN.CropImgRequestCode);
    }

    /**
     * bitmap保存到本地
     *
     * @param bmp
     */
    private File saveCroppedImage(Bitmap bmp) {
        File file = new File(CHEN.crop_img_path);
        if (!file.exists())
            file.mkdir();

        file = new File(CHEN.crop_img_path + "/" + "crop_temp.jpg");
        String fileName = file.getName();
        String mName = fileName.substring(0, fileName.lastIndexOf("."));
        String sName = fileName.substring(fileName.lastIndexOf("."));

        Log.e("mName", mName);
        Log.e("sName", sName);

        //裁剪后的图片的地址
        String newFilePath = CHEN.crop_img_path + "/" + mName + "_cropped" + sName;
        file = new File(newFilePath);
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值