android拍照压缩及添加水印3

来源:互联网 发布:智百威软件免费下载 编辑:程序博客网 时间:2024/05/22 07:08


1.调用拍照(获取原始图片)


[java] view plaincopyprint?
  1. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  2. String ppName = "example_2012.jpg";  
  3. File f1 = new File("/sdcard/mfile/image/" + ppName);  
  4. Uri u1 = Uri.fromFile(f1);  
  5. intent.putExtra(MediaStore.EXTRA_OUTPUT, u1);  
  6. startActivityForResult(intent, CAMERA_ACTIVITY);  



2.获取本地图片进行处理并回写


[java] view plaincopyprint?
  1. private void dealPhotoFile(final String file)  
  2. {  
  3.     PhotoTask task = new PhotoTask(file);  
  4.     task.start();  
  5.       
  6.     photoTasks.add(task);  
  7. }  


[java] view plaincopyprint?
  1. private class PhotoTask extends Thread  
  2. {  
  3.     private String file;  
  4.       
  5.     private boolean isFinished;  
  6.       
  7.     public PhotoTask(String file)  
  8.     {  
  9.         this.file = file;  
  10.     }  
  11.       
  12.     @Override  
  13.     public void run()  
  14.     {  
  15.         BufferedOutputStream bos = null;  
  16.         Bitmap icon = null;  
  17.         try  
  18.         {  
  19.             BitmapFactory.Options options = new BitmapFactory.Options();  
  20.             options.inJustDecodeBounds = true;  
  21.             BitmapFactory.decodeFile(file, options); //此时返回bm为空  
  22.             float percent =  
  23.                 options.outHeight > options.outWidth ? options.outHeight / 960f : options.outWidth / 960f;  
  24.               
  25.             if (percent < 1)  
  26.             {  
  27.                 percent = 1;  
  28.             }  
  29.             int width = (int)(options.outWidth / percent);  
  30.             int height = (int)(options.outHeight / percent);  
  31.             icon = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
  32.               
  33.             //初始化画布 绘制的图像到icon上    
  34.             Canvas canvas = new Canvas(icon);  
  35.             //建立画笔    
  36.             Paint photoPaint = new Paint();  
  37.             //获取跟清晰的图像采样    
  38.             photoPaint.setDither(true);  
  39.             //过滤一些    
  40.             //                    photoPaint.setFilterBitmap(true);  
  41.             options.inJustDecodeBounds = false;  
  42.               
  43.             Bitmap prePhoto = BitmapFactory.decodeFile(file);  
  44.             if (percent > 1)  
  45.             {  
  46.                 prePhoto = Bitmap.createScaledBitmap(prePhoto, width, height, true);  
  47.             }  
  48.               
  49.             canvas.drawBitmap(prePhoto, 00, photoPaint);  
  50.               
  51.             if (prePhoto != null && !prePhoto.isRecycled())  
  52.             {  
  53.                 prePhoto.recycle();  
  54.                 prePhoto = null;  
  55.                 System.gc();  
  56.             }  
  57.               
  58.             //设置画笔    
  59.             Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);  
  60.             //字体大小    
  61.             textPaint.setTextSize(20.0f);  
  62.             //采用默认的宽度    
  63.             textPaint.setTypeface(Typeface.DEFAULT);  
  64.             //采用的颜色    
  65.             textPaint.setColor(Color.YELLOW);  
  66.             //阴影设置    
  67.             //                textPaint.setShadowLayer(3f, 1, 1, Color.DKGRAY);    
  68.               
  69.             // 时间水印    
  70.             String mark = getCurrTime("yyyy-MM-dd HH:mm:ss");  
  71.             float textWidth = textPaint.measureText(mark);  
  72.             canvas.drawText(mark, width - textWidth - 10, height - 26, textPaint);  
  73.               
  74.             bos = new BufferedOutputStream(new FileOutputStream(file));  
  75.               
  76.             int quaility = (int)(100 / percent > 80 ? 80 : 100 / percent);  
  77.             icon.compress(CompressFormat.JPEG, quaility, bos);  
  78.             bos.flush();  
  79.         }  
  80.         catch (Exception e)  
  81.         {  
  82.             e.printStackTrace();  
  83.         }  
  84.         finally  
  85.         {  
  86.             isFinished = true;  
  87.             if (bos != null)  
  88.             {  
  89.                 try  
  90.                 {  
  91.                     bos.close();  
  92.                 }  
  93.                 catch (IOException e)  
  94.                 {  
  95.                     e.printStackTrace();  
  96.                 }  
  97.             }  
  98.             if (icon != null && !icon.isRecycled())  
  99.             {  
  100.                 icon.recycle();  
  101.                 icon = null;  
  102.                 System.gc();  
  103.             }  
  104.         }  
  105.     }  
  106. }  



[java] view plaincopyprint?
  1. private static String getCurrTime(String pattern)  
  2. {  
  3.     if (pattern == null)  
  4.     {  
  5.         pattern = "yyyyMMddHHmmss";  
  6.     }  
  7.     return (new SimpleDateFormat(pattern)).format(new Date());  
  8. }  


以下转自eoe关于图片内存溢出的问题处理分享(没有实际验证)

腾讯高级开发工程师-郭大扬【Android图像处理】

实际问题解决分案分享


最有效方法
options.inPurgeable = true;
options.inInputShareable = true;

 

接下来讲一个比较细节的问题,我们做图像处理,老是会遇到out of memory,实在很抓狂,有时候搞半天都不知道为什么。搜索了上百个帖子都说把它压成一半,实际上我就不想压成一半,我就要这么大。我这里摸索到一个比较好的解决方法。我自己觉得百试不爽的加上这两句属性,一个是代表可以擦除的,一个代表可以共享,我现在一般不会遇到内存溢出的情况。
0 0