Android拍照或从相册选取以及裁剪

本文介绍如何在Android应用中实现拍照、从相册选择图片,并进行裁剪的功能。同时,讨论了如何使用ThumbnailUtils类来压缩Bitmap到指定大小,以适应不同的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private void getPicFromPhoto() {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                "image/*");
        startActivityForResult(intent, PHOTO_REQUEST);
    }

    private void getPicFromCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //intent.putExtra("android.intent.extras.CAMERA_FACING", 1); // 调用前置摄像头
        // 下面这句指定调用相机拍照后的照片存储的路径
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
                Environment.getExternalStorageDirectory(), "test.jpg")));
        startActivityForResult(intent, CAMERA_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case CAMERA_REQUEST:
            switch (resultCode) {
            case -1:// -1表示拍照成功
                File file = new File(Environment.getExternalStorageDirectory()
                        + "/test.jpg");
                if (file.exists()) {
                    photoClip(Uri.fromFile(file));
                }
                break;
            default:
                break;
            }
            break;
        case PHOTO_REQUEST:
            if (data != null) {
                photoClip(data.getData());
            }
            break;
        case PHOTO_CLIP:
            if (data != null) {
                Bundle extras = data.getExtras();
                if (extras != null) {
                    Bitmap photo = extras.getParcelable("data");
                    faceImage.setImageBitmap(photo);
                }
            }
            break;
        default:
            break;
        }

    }

    private void photoClip(Uri uri) {
        // 调用系统中自带的图片剪裁
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, PHOTO_CLIP);
    }

public static Bitmap resizeImage(String path,
                                      int width, int height)
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;//不加载bitmap到内存中
        BitmapFactory.decodeFile(path,options);
        int outWidth = options.outWidth;
        int outHeight = options.outHeight;
        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inSampleSize = 1;

        if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0)
        {
            int sampleSize= (int) (Math.ceil((outWidth/width+outHeight/height)/2));
            Logger.d( "sampleSize = " + sampleSize);
            options.inSampleSize = 4;
        }

        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }

将bitmap压缩到指定大小:
从Android 2.2开始系统新增了一个缩略图ThumbnailUtils类,位于framework包下的android.media.ThumbnailUtils位置,可以帮助我们从mediaprovider中获取系统中的视频或图片文件的缩略图,该类提供了三种静态方法可以直接调用获取。

/** 
 *  
 * 创建一个指定大小的缩略图 
 * @param source 源文件(Bitmap类型) 
 * @param width  压缩成的宽度 
 * @param height 压缩成的高度 
 */  
ThumbnailUtils.extractThumbnail(source, width, height);


/** 
 * 创建一个指定大小居中的缩略图 
 *  
 * @param source 源文件(Bitmap类型) 
 * @param width  输出缩略图的宽度 
 * @param height 输出缩略图的高度 
 * @param options 如果options定义为OPTIONS_RECYCLE_INPUT,则回收@param source这个资源文件 
 * (除非缩略图等于@param source) 
 *  
 */  
 ThumbnailUtils.extractThumbnail(source, width, height, options);  


/** 
 * 创建一张视频的缩略图 
 * 如果视频已损坏或者格式不支持可能返回null 
 *  
 * @param filePath 视频文件路径  如:/sdcard/android.3gp 
 * @param kind kind可以为MINI_KIND或MICRO_KIND 
 *  
 */  
ThumbnailUtils.createVideoThumbnail(filePath, kind);  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值