Android图片 - litonghui/TechBlog GitHub Wiki
1,获取图片大小BitmapFactory.Options
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
其中inJustDecodeBounds ,If set to true, the decoder will return null (no bitmap)
使用这种方式获取图片大小原因是,不会产生多余的BitMap,通过options.outWidth 和 options.outHeigh 获取想要图片的高度。
2,图片旋转ExifInterface,Android对图片处理旋转放法如下:
ExifInterface exif;
try {
exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
Matrix matrix = new Matrix();
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
matrix.postRotate(90);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
matrix.postRotate(180);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
matrix.postRotate(270);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
true);
} catch (IOException e) {
e.printStackTrace();
}
3,Bitmap 与 Byte 之间转换:
public static byte[] BitmapToBytes(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,stream);
return stream.toByteArray();
}
public static Bitmap BytesToBitmap(byte[] bytes){
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
4,图片部分透明度设置,通过获取argb,位运算,降低透明度。
/**
* 设置图片 Bitmap任意透明度
*
* @param org
* @param number 的范围是0-100,0表示完全透明即完全看不到
* @param positioin 1/2;1/4 等 范围0-1,1表示整个图片
* @return
*/
public static Bitmap getTransparentBitmap(Bitmap org, int number, double positioin) {
if (org != null) {
int[] argb = new int[org.getWidth() * org.getHeight()];
org.getPixels(argb, 0, org.getWidth(), 0, 0, org.getWidth(), org.getHeight());
// 获得图片的ARGB值
number = number * 255 / 100;
for (int i = (int) (argb.length * positioin); i < argb.length; i++) {//
argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF);
}
org = Bitmap.createBitmap(argb, org.getWidth(),
org.getHeight(), Config.ARGB_8888);
return org;
}
return org;
}