拍照+剪裁+bitmap
来源:互联网 发布:js div弹窗 编辑:程序博客网 时间:2024/06/17 20:31
启动相机拍摄
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //下面这句指定调用相机拍照后的照片存储的路径 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri .fromFile(new File(Environment.getExternalStorageDirectory(), "xiaoma.jpg")));
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 stubfloat 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溢出问题
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(); }
- 拍照+剪裁+bitmap
- # Bitmap 缩放 剪裁
- 拍照,选择照片并且剪裁
- Android 7.0 拍照后剪裁
- Bitmap缩放剪裁便宜旋转
- Android 按照比例剪裁图片,以及拍照并剪裁
- android拍照图片选取与图片剪裁
- android 调用系统照相机拍照后剪裁
- android选择图片、拍照、并剪裁
- android:拍照,相册剪裁的实现方式
- javacv 颜色识别&&拍照&&剪裁&&图像旋转
- 个人中心模块-拍照剪裁上传
- Bitmap/byte/Drawble 相互转换 + 内存剪裁
- android调用照相机拍照获取照片并做简单剪裁
- Android调用手机拍照以及从相册选择照片,剪裁
- android调用照相机拍照获取照片并做简单剪裁
- Android拍照后先剪裁再显示图片
- android调用照相机拍照获取照片并做简单剪裁
- TI-Davinci开发系列之三测试XDS560V2仿真器与DM3730的正确连接
- 【机器学习-斯坦福】学习笔记21——增强学习(Reinforcement Learning and Control)
- 部署ArcGIS JS API 离线包(Tomcat与IIS)
- Android 如何制定FM搜台起始频率点 M
- getLayeredPane与getContentPane的区别
- 拍照+剪裁+bitmap
- ubuntu修改mac地址
- sprint3
- 使用Android自带DownloadManager下载文件
- 写论文
- Java性能优化技巧集锦
- 最牛B的编码套路
- 图像处理与计算机视觉 基础、经典以及最近发展
- 万能的一玄:一玄创业故事(一)