拍照+剪裁+bitmap

启动相机拍摄
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
//下面这句指定调用相机拍照后的照片存储的路径 
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri .fromFile(
new File(Environment.getExternalStorageDirectory(),  "xiaoma.jpg"))); 
 裁剪图片方法实现 ,也是调用系统的Crop实现
 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); 
1.资源命名问题:不要以a开头,否侧经常会加载不到。
2.值拍照一次就立刻返回。

    在camera中,点击拍照调的是doAttach。后来查看源码,发现onPictureTaken里面有调用到doAttach,只是有个判断,mQuickCapture,原来是intent里面的一个boolean值,只是这个EXTRA_QUICK_CAPTURE是私有的变量,于是复制到需要的地方,直接加到intent里面,这样在onPictureTaken里面直接调用doAttach,达到目的。

3.当裁剪图片小于规定的大小时,会存在毛边

添加下面2个字段可以解决问题。

putExtra("scale", true)//黑边
putExtra("scaleUpIfNeeded", true)//黑边

剪裁图片成圆形

        /** 
         * 转换图片成圆形 
         * @param bitmap 传入Bitmap对象 
         * @return 
         */
        public Bitmap toRoundBitmap(Bitmap bitmap) { 
                int width = bitmap.getWidth(); 
                int height = bitmap.getHeight(); 
                float roundPx; 
                float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom; 
                if (width <= height) { 
                        roundPx = width / 2; 
                        top = 0; 
                        bottom = width; 
                        left = 0; 
                        right = width; 
                        height = width; 
                        dst_left = 0; 
                        dst_top = 0; 
                        dst_right = width; 
                        dst_bottom = width; 
                } else { 
                        roundPx = height / 2; 
                        float clip = (width - height) / 2; 
                        left = clip; 
                        right = width - clip; 
                        top = 0; 
                        bottom = height; 
                        width = height; 
                        dst_left = 0; 
                        dst_top = 0; 
                        dst_right = height; 
                        dst_bottom = height; 
                } 
                  
                Bitmap output = Bitmap.createBitmap(width, 
                                height, Config.ARGB_8888); 
                Canvas canvas = new Canvas(output); 
                  
                final int color = 0xff424242; 
                final Paint paint = new Paint(); 
                final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom); 
                final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom); 
                final RectF rectF = new RectF(dst); 
  
                paint.setAntiAlias(true); 
                  
                canvas.drawARGB(0, 0, 0, 0); 
                paint.setColor(color); 
//drawRoundRect用于画圆角矩形,第一个参数为显示区域,后面两个参数分别是水平圆角半径和垂直圆角半径
                canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
  
                paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
//从源图片裁剪显示挖取的图片
                canvas.drawBitmap(bitmap, src, dst, paint); 
                return output; 
        }

图片缩放

//按比例缩放图片
	private Bitmap scaleBtmap(Bitmap a, int width, int height) {
		// TODO Auto-generated method stub
		
		float scale_x = (float)width/a.getWidth();
		float scale_y = (float)height/a.getHeight();
		Matrix matrix = new Matrix();
		matrix.postScale(scale_x, scale_y);
		
		Bitmap aa = Bitmap.createBitmap(a, 0, 0, a.getWidth(), a.getHeight(), matrix, true);
		
		return aa;
	}

或者直接使用ThumbnailUtils类的静态方法
extractThumbnail(Bitmap,width,height,ThumbnailUtils.OPTIONS_RECYCLE_INPUT)方法
android.media.ThumbnailUtils提供了3个静态方法
   static Bitmap  createVideoThumbnail(String filePath, int kind)  //获取视频文件的缩略图,第一个参数为视频文件的位置,比如/sdcard/android123.3gp,而第二个参数可以为MINI_KIND或MICRO_KIND最终和分辨率有关 
   static Bitmap  extractThumbnail(Bitmap source, int width, int height, int options)  //直接对Bitmap进行缩略操作,最后一个参数定义为OPTIONS_RECYCLE_INPUT ,来回收资源
   static Bitmap  extractThumbnail(Bitmap source, int width, int height) // 这个和上面的方法一样,无options选项

bitmap溢出问题

需要注意的是,由于bitmap占内存很大,注意释放和创建,以免出现内存溢出OOM。
1 bitmap如果不用了,回收掉
protected void onDestroy() {  
        super.onDestroy();  
        if(bmp1 != null){  
            bmp1.recycle();  
            bmp1 = null;  
        }  
        if(bmp2 != null){  
            bmp2.recycle();  
            bmp2 = null;  
        }  
    }  
2 先算出该bitmap的大小,然后通过调节采样率的方式来规避
BitmapFactory.Options opts = new BitmapFactory.Options();  
        opts.inJustDecodeBounds = true;  
        BitmapFactory.decodeFile(imageFile, opts);  
        opts.inSampleSize = computeSampleSize(opts, minSideLength, maxNumOfPixels);  
        opts.inJustDecodeBounds = false;  
        try {  
            return BitmapFactory.decodeFile(imageFile, opts);  
        } catch (OutOfMemoryError err) {  
        }  
        return null;  
3 在进行文件传输时,最好采用压缩的方式变成byte[]再传输
public static byte[] bitmap2Bytes(Bitmap bm) {  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        bm.compress(Bitmap.CompressFormat.JPEG, 90, baos);  
        return baos.toByteArray();  
    }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值