Android 图片的处理方法 更新中
1 // 压缩图片大小 2 public static Bitmap compressImage(Bitmap image) { 3 4 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 5 image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 将压缩的图片存到baos中 6 // >100kb go on compress 7 int option = 100; 8 while (baos.toByteArray().length / 1024 > 100) { 9 baos.reset(); 10 image.compress(Bitmap.CompressFormat.JPEG, option, baos); 11 option -= 10; 12 } 13 14 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中 15 Bitmap bitmap = BitmapFactory.decodeStream(bais, null, null);// 把ByteArrayInputStream数据生成图片 16 17 return bitmap; 18 } 19 20 21 /** 22 * 将彩色图转换为灰度图 23 * @param img 位图 24 * @return 返回转换好的位图 25 */ 26 public Bitmap convertGreyImg(Bitmap img) { 27 int width = img.getWidth(); //获取位图的宽 28 int height = img.getHeight(); //获取位图的高 29 30 int []pixels = new int[width * height]; //通过位图的大小创建像素点数组 31 32 img.getPixels(pixels, 0, width, 0, 0, width, height); 33 int alpha = 0xFF << 24; 34 for(int i = 0; i < height; i++) { 35 for(int j = 0; j < width; j++) { 36 int grey = pixels[width * i + j]; 37 38 int red = ((grey & 0x00FF0000 ) >> 16); 39 int green = ((grey & 0x0000FF00) >> 8); 40 int blue = (grey & 0x000000FF); 41 42 grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11); 43 grey = alpha | (grey << 16) | (grey << 8) | grey; 44 pixels[width * i + j] = grey; 45 } 46 } 47 Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565); 48 result.setPixels(pixels, 0, width, 0, 0, width, height); 49 return result; 50 }
public static Bitmap decodeFile (String pathName)
Added in API level 1
Decode a file path into a bitmap. If the specified file name is null, or cannot be decoded into a bitmap, the function returns null.
Parameters
pathName | complete path name for the file to be decoded. |
---|
Returns
- the resulting decoded bitmap, or null if it could not be decoded.
/** * 放大缩小图片 */ public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidht = ((float) w / width); float scaleHeight = ((float) h / height); matrix.postScale(scaleWidht, scaleHeight); Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); return newbmp; }