Android 图片操作(Bitmap)

来源:互联网 发布:php 无法设置cookie 编辑:程序博客网 时间:2024/05/01 14:38
    博客分类:
  • Android
[java] view plaincopy
  1. /** 
  2.      * 将多个Bitmap合并成一个图片。 
  3.      *  
  4.      * @param int 将多个图合成多少列 
  5.      * @param Bitmap... 要合成的图片 
  6.      * @return 
  7.      */  
  8. public static Bitmap combineBitmaps(int columns, Bitmap... bitmaps) {  
  9.         if (columns <= 0 || bitmaps == null || bitmaps.length == 0) {  
  10.             throw new IllegalArgumentException("Wrong parameters: columns must > 0 and bitmaps.length must > 0.");  
  11.         }  
  12.         int maxWidthPerImage = 0;  
  13.         int maxHeightPerImage = 0;  
  14.         for (Bitmap b : bitmaps) {  
  15.             maxWidthPerImage = maxWidthPerImage > b.getWidth() ? maxWidthPerImage : b.getWidth();  
  16.             maxHeightPerImage = maxHeightPerImage > b.getHeight() ? maxHeightPerImage : b.getHeight();  
  17.         }  
  18.         int rows = 0;  
  19.         if (columns >= bitmaps.length) {  
  20.             rows = 1;  
  21.             columns = bitmaps.length;  
  22.         } else {  
  23.             rows = bitmaps.length % columns == 0 ? bitmaps.length / columns : bitmaps.length / columns + 1;  
  24.         }  
  25.         Bitmap newBitmap = Bitmap.createBitmap(columns * maxWidthPerImage, rows * maxHeightPerImage, Config.RGB_565);  
  26.   
  27.         for (int x = 0; x < rows; x++) {  
  28.             for (int y = 0; y < columns; y++) {  
  29.                 int index = x * columns + y;  
  30.                 if (index >= bitmaps.length)  
  31.                     break;  
  32.                 newBitmap = mixtureBitmap(newBitmap, bitmaps[index], new PointF(y * maxWidthPerImage, x * maxHeightPerImage));  
  33.             }  
  34.         }  
  35.         return newBitmap;  
  36.     }  


[java] view plaincopy
  1. /** 
  2.      * Mix two Bitmap as one. 
  3.      *  
  4.      * @param bitmapOne 
  5.      * @param bitmapTwo 
  6.      * @param point 
  7.      *            where the second bitmap is painted. 
  8.      * @return 
  9.      */  
  10.     public static Bitmap mixtureBitmap(Bitmap first, Bitmap second, PointF fromPoint) {  
  11.         if (first == null || second == null || fromPoint == null) {  
  12.             return null;  
  13.         }  
  14.         Bitmap newBitmap = Bitmap.createBitmap(first.getWidth(), first.getHeight(), Config.ARGB_4444);  
  15.         Canvas cv = new Canvas(newBitmap);  
  16.         cv.drawBitmap(first, 00null);  
  17.         cv.drawBitmap(second, fromPoint.x, fromPoint.y, null);  
  18.         cv.save(Canvas.ALL_SAVE_FLAG);  
  19.         cv.restore();  
  20.         return newBitmap;  
  21.     }  


[java] view plaincopy
  1. //截屏  
  2. public static Bitmap getScreenshotsForCurrentWindow(Activity activity) {  
  3.         View cv = activity.getWindow().getDecorView();  
  4.         Bitmap bmp = Bitmap.createBitmap(cv.getWidth(), cv.getHeight(), Bitmap.Config.ARGB_4444);  
  5.         cv.draw(new Canvas(bmp));  
  6.         return bmp;  
  7.     }  


[java] view plaincopy
  1. //旋转图片  
  2. // Rotates the bitmap by the specified degree.  
  3. // If a new bitmap is created, the original bitmap is recycled.  
  4. public static Bitmap rotate(Bitmap b, int degrees) {  
  5.     if (degrees != 0 && b != null) {  
  6.         Matrix m = new Matrix();  
  7.         m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);  
  8.         try {  
  9.             b = Bitmap.createBitmap(b, 00, b.getWidth(), b.getHeight(), m, true);  
  10.              if (b != b2) {  
  11.              b.recycle();  
  12.              b = b2;  
  13.              }  
  14.         } catch (OutOfMemoryError ex) {  
  15.             // We have no memory to rotate. Return the original bitmap.  
  16.         }  
  17.     }  
  18.         return b;  
  19. }  


[java] view plaincopy
  1. //可用于生成缩略图。  
  2. /** 
  3.  * Creates a centered bitmap of the desired size. Recycles the input. 
  4.  *  
  5.  * @param source 
  6.  */  
  7. public static Bitmap extractMiniThumb(Bitmap source, int width, int height) {  
  8.     return extractMiniThumb(source, width, height, true);  
  9. }  
  10.   
  11. public static Bitmap extractMiniThumb(Bitmap source, int width, int height, boolean recycle) {  
  12.     if (source == null) {  
  13.         return null;  
  14.     }  
  15.   
  16.     float scale;  
  17.     if (source.getWidth() < source.getHeight()) {  
  18.         scale = width / (float) source.getWidth();  
  19.     } else {  
  20.         scale = height / (float) source.getHeight();  
  21.     }  
  22.     Matrix matrix = new Matrix();  
  23.     matrix.setScale(scale, scale);  
  24.     Bitmap miniThumbnail = transform(matrix, source, width, height, false);  
  25.   
  26.     if (recycle && miniThumbnail != source) {  
  27.         source.recycle();  
  28.     }  
  29.     return miniThumbnail;  
  30. }  
  31.   
  32. public static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean scaleUp) {  
  33.         int deltaX = source.getWidth() - targetWidth;  
  34.         int deltaY = source.getHeight() - targetHeight;  
  35.         if (!scaleUp && (deltaX < 0 || deltaY < 0)) {  
  36.             /* 
  37.              * In this case the bitmap is smaller, at least in one dimension, 
  38.              * than the target. Transform it by placing as much of the image as 
  39.              * possible into the target and leaving the top/bottom or left/right 
  40.              * (or both) black. 
  41.              */  
  42.             Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);  
  43.             Canvas c = new Canvas(b2);  
  44.   
  45.             int deltaXHalf = Math.max(0, deltaX / 2);  
  46.             int deltaYHalf = Math.max(0, deltaY / 2);  
  47.             Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf  
  48.                     + Math.min(targetHeight, source.getHeight()));  
  49.             int dstX = (targetWidth - src.width()) / 2;  
  50.             int dstY = (targetHeight - src.height()) / 2;  
  51.             Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY);  
  52.             c.drawBitmap(source, src, dst, null);  
  53.             return b2;  
  54.         }  
  55.         float bitmapWidthF = source.getWidth();  
  56.         float bitmapHeightF = source.getHeight();  
  57.   
  58.         float bitmapAspect = bitmapWidthF / bitmapHeightF;  
  59.         float viewAspect = (float) targetWidth / targetHeight;  
  60.   
  61.         if (bitmapAspect > viewAspect) {  
  62.             float scale = targetHeight / bitmapHeightF;  
  63.             if (scale < .9F || scale > 1F) {  
  64.                 scaler.setScale(scale, scale);  
  65.             } else {  
  66.                 scaler = null;  
  67.             }  
  68.         } else {  
  69.             float scale = targetWidth / bitmapWidthF;  
  70.             if (scale < .9F || scale > 1F) {  
  71.                 scaler.setScale(scale, scale);  
  72.             } else {  
  73.                 scaler = null;  
  74.             }  
  75.         }  
  76.   
  77.         Bitmap b1;  
  78.         if (scaler != null) {  
  79.             // this is used for minithumb and crop, so we want to filter here.  
  80.             b1 = Bitmap.createBitmap(source, 00, source.getWidth(), source.getHeight(), scaler, true);  
  81.         } else {  
  82.             b1 = source;  
  83.         }  
  84.   
  85.         int dx1 = Math.max(0, b1.getWidth() - targetWidth);  
  86.         int dy1 = Math.max(0, b1.getHeight() - targetHeight);  
  87.   
  88.         Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight);  
  89.   
  90.         if (b1 != source) {  
  91.             b1.recycle();  
  92.         }  
  93.   
  94.         return b2;  
  95.     }  


[java] view plaincopy
  1. //图片剪切  
  2. public static Bitmap cutBitmap(Bitmap mBitmap, Rect r, Bitmap.Config config) {  
  3.     int width = r.width();  
  4.     int height = r.height();  
  5.   
  6.     Bitmap croppedImage = Bitmap.createBitmap(width, height, config);  
  7.   
  8.     Canvas cvs = new Canvas(croppedImage);  
  9.     Rect dr = new Rect(00, width, height);  
  10.     cvs.drawBitmap(mBitmap, r, dr, null);  
  11.     return croppedImage;  
  12. }  


[java] view plaincopy
  1. //从任一Drawable得到Bitmap  
  2. public static Bitmap drawableToBitmap(Drawable drawable) {  
  3.     Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.RGB_565);  
  4.     Canvas canvas = new Canvas(bitmap);  
  5.     drawable.setBounds(00, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
  6.     drawable.draw(canvas);  
  7.     return bitmap;  
  8. }  


[java] view plaincopy
  1. /** 
  2.      * Save Bitmap to a file.保存图片到SD卡。 
  3.      *  
  4.      * @param bitmap 
  5.      * @param file 
  6.      * @return error message if the saving is failed. null if the saving is 
  7.      *         successful. 
  8.      * @throws IOException 
  9.      */  
  10.     public static void saveBitmapToFile(Bitmap bitmap, String _file) throws IOException {  
  11.         BufferedOutputStream os = null;  
  12.         try {  
  13.             File file = new File(_file);  
  14.             // String _filePath_file.replace(File.separatorChar +  
  15.             // file.getName(), "");  
  16.             int end = _file.lastIndexOf(File.separator);  
  17.             String _filePath = _file.substring(0, end);  
  18.             File filePath = new File(_filePath);  
  19.             if (!filePath.exists()) {  
  20.                 filePath.mkdirs();  
  21.             }  
  22.             file.createNewFile();  
  23.             os = new BufferedOutputStream(new FileOutputStream(file));  
  24.             bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);  
  25.         } finally {  
  26.             if (os != null) {  
  27.                 try {  
  28.                     os.close();  
  29.                 } catch (IOException e) {  
  30.                     Log.e(TAG_ERROR, e.getMessage(), e);  
  31.                 }  
  32.             }  
  33.         }  
  34.     }