android拍照压缩及添加水印3
来源:互联网 发布:智百威软件免费下载 编辑:程序博客网 时间:2024/05/22 07:08
1.调用拍照(获取原始图片)
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- String ppName = "example_2012.jpg";
- File f1 = new File("/sdcard/mfile/image/" + ppName);
- Uri u1 = Uri.fromFile(f1);
- intent.putExtra(MediaStore.EXTRA_OUTPUT, u1);
- startActivityForResult(intent, CAMERA_ACTIVITY);
2.获取本地图片进行处理并回写
- private void dealPhotoFile(final String file)
- {
- PhotoTask task = new PhotoTask(file);
- task.start();
- photoTasks.add(task);
- }
- private class PhotoTask extends Thread
- {
- private String file;
- private boolean isFinished;
- public PhotoTask(String file)
- {
- this.file = file;
- }
- @Override
- public void run()
- {
- BufferedOutputStream bos = null;
- Bitmap icon = null;
- try
- {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(file, options); //此时返回bm为空
- float percent =
- options.outHeight > options.outWidth ? options.outHeight / 960f : options.outWidth / 960f;
- if (percent < 1)
- {
- percent = 1;
- }
- int width = (int)(options.outWidth / percent);
- int height = (int)(options.outHeight / percent);
- icon = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
- //初始化画布 绘制的图像到icon上
- Canvas canvas = new Canvas(icon);
- //建立画笔
- Paint photoPaint = new Paint();
- //获取跟清晰的图像采样
- photoPaint.setDither(true);
- //过滤一些
- // photoPaint.setFilterBitmap(true);
- options.inJustDecodeBounds = false;
- Bitmap prePhoto = BitmapFactory.decodeFile(file);
- if (percent > 1)
- {
- prePhoto = Bitmap.createScaledBitmap(prePhoto, width, height, true);
- }
- canvas.drawBitmap(prePhoto, 0, 0, photoPaint);
- if (prePhoto != null && !prePhoto.isRecycled())
- {
- prePhoto.recycle();
- prePhoto = null;
- System.gc();
- }
- //设置画笔
- Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
- //字体大小
- textPaint.setTextSize(20.0f);
- //采用默认的宽度
- textPaint.setTypeface(Typeface.DEFAULT);
- //采用的颜色
- textPaint.setColor(Color.YELLOW);
- //阴影设置
- // textPaint.setShadowLayer(3f, 1, 1, Color.DKGRAY);
- // 时间水印
- String mark = getCurrTime("yyyy-MM-dd HH:mm:ss");
- float textWidth = textPaint.measureText(mark);
- canvas.drawText(mark, width - textWidth - 10, height - 26, textPaint);
- bos = new BufferedOutputStream(new FileOutputStream(file));
- int quaility = (int)(100 / percent > 80 ? 80 : 100 / percent);
- icon.compress(CompressFormat.JPEG, quaility, bos);
- bos.flush();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- isFinished = true;
- if (bos != null)
- {
- try
- {
- bos.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- if (icon != null && !icon.isRecycled())
- {
- icon.recycle();
- icon = null;
- System.gc();
- }
- }
- }
- }
- private static String getCurrTime(String pattern)
- {
- if (pattern == null)
- {
- pattern = "yyyyMMddHHmmss";
- }
- return (new SimpleDateFormat(pattern)).format(new Date());
- }
以下转自eoe关于图片内存溢出的问题处理分享(没有实际验证)
腾讯高级开发工程师-郭大扬【Android图像处理】
实际问题解决分案分享
最有效方法
options.inPurgeable = true;
options.inInputShareable = true;
接下来讲一个比较细节的问题,我们做图像处理,老是会遇到out of memory,实在很抓狂,有时候搞半天都不知道为什么。搜索了上百个帖子都说把它压成一半,实际上我就不想压成一半,我就要这么大。我这里摸索到一个比较好的解决方法。我自己觉得百试不爽的加上这两句属性,一个是代表可以擦除的,一个代表可以共享,我现在一般不会遇到内存溢出的情况。
0 0
- android拍照压缩及添加水印3
- android拍照压缩及添加水印3
- Android调用相机拍照并添加水印
- 微信浏览器内兼容android iOS调取手机摄像头进行拍照、打水印、压缩、预览
- java 压缩图片 添加水印
- Android:图片添加水印
- Android 图片添加水印
- Android添加图片水印
- Android 图片添加水印
- Android 添加水印相册
- Android添加水印效果
- Android添加图片水印
- Android图片压缩、加水印
- Thumbnailator实现图片压缩,旋转,添加水印
- 上传图片及添加水印
- android拍照保存被压缩
- android 拍照并压缩照片
- Android压缩图片并保持不失真的高效方法以及为图片添加水印
- HTML5小游戏 -- 格罗斯方块
- Java2Word
- 1009. Product of Polynomials (25)
- 评教系统——学生评教提交
- codeforces 417D Cunning Gena
- android拍照压缩及添加水印3
- oracle 数据类型详解---日期型
- CF31D 分治
- 1010. Radix (25)
- Flatten Binary Tree to Linked List
- Swift中的!和?
- 1011. World Cup Betting (20)
- SQL Server不区分大小写的问题
- 图像处理+机器学习相关资源整合