您的位置:首页 > 移动开发 > Android开发

Android 图片的处理方法 更新中

2014-08-20 09:33 183 查看
// 压缩图片大小
public static Bitmap compressImage(Bitmap image) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 将压缩的图片存到baos中
// >100kb go on compress
int option = 100;
while (baos.toByteArray().length / 1024 > 100) {
baos.reset();
image.compress(Bitmap.CompressFormat.JPEG, option, baos);
option -= 10;
}

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(bais, null, null);// 把ByteArrayInputStream数据生成图片

return bitmap;
}

/**
* 将彩色图转换为灰度图
* @param img 位图
* @return  返回转换好的位图
*/
public Bitmap convertGreyImg(Bitmap img) {
int width = img.getWidth();         //获取位图的宽
int height = img.getHeight();       //获取位图的高

int []pixels = new int[width * height]; //通过位图的大小创建像素点数组

img.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for(int i = 0; i < height; i++)  {
for(int j = 0; j < width; j++) {
int grey = pixels[width * i + j];

int red = ((grey  & 0x00FF0000 ) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);

grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565);
result.setPixels(pixels, 0, width, 0, 0, width, height);
return result;
}


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
pathNamecomplete 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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: