-
打开图片
图片的打开主要是把各种格式的图片转换为Bitmap对象,Android通过BitmapFactory类提供了一系列的静态方法来协助完成这个操作,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class BitmapFactory {
public static Bitmap decodeFile(String pathName, Options opts);
public static Bitmap decodeFile(String pathName);
public static Bitmap decodeResourceStream(Resources res, TypedValue value,
InputStream is, Rect pad, Options opts) ;
public static Bitmap decodeResource(Resources res, int id, Options opts) ;
public static Bitmap decodeResource(Resources res, int id);
public static Bitmap decodeByteArray( byte [] data, int offset, int length, Options opts);
public static Bitmap decodeByteArray( byte [] data, int offset, int length);
public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts);
public static Bitmap decodeStream(InputStream is) ;
public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts);
public static Bitmap decodeFileDescriptor(FileDescriptor fd) ;
} |
通过这些静态方法,我们可以方便地从文件、资源、字节流等各种途径打开图片,生成Bitmap对象。下面给出一个从文件中打开图片的函数封装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static Bitmap load( String filepath ) {
Bitmap bitmap = null ;
try {
FileInputStream fin = new FileInputStream(filepath);
bitmap = BitmapFactory.decodeStream(fin);
fin.close();
}
catch (FileNotFoundException e) {
}
catch (IOException e) {
}
return bitmap;
} |
2. 保存图片
图片的保存则主要通过Bitmap的compress方法,该方法的原型如下:
1
2
3
4
5
6
7
8
9
10
11
|
/** * Write a compressed version of the bitmap to the specified outputstream.
* @param format The format of the compressed image
* @param quality Hint to the compressor, 0-100. 0 meaning compress for
* small size, 100 meaning compress for max quality. Some
* formats, like PNG which is lossless, will ignore the
* quality setting
* @param stream The outputstream to write the compressed data.
* @return true if successfully compressed to the specified stream.
*/
public boolean compress(CompressFormat format, int quality, OutputStream stream)
|
第一个参数是图片格式,只有JPEG、PNG和WEBP三种,第二个参数是压缩质量(0~100),数值越大图片信息损失越小,第三个参数则是文件流对象。
同样,这里给出一个保存图片的函数封装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public static void save( Bitmap bitmap, String filepath ) {
try {
FileOutputStream fos = new FileOutputStream(filepath);
bitmap.compress(CompressFormat.JPEG, 100 , fos);
bitmap.recycle();
fos.close();
}
catch (FileNotFoundException e) {
}
catch (IOException e) {
}
}
|
3. 剪裁图片
Android中剪裁图片主要有2种方法,一种通过Bitmap的createBitmap方法来生成剪裁的图片,另一种则是通过Canvas对象来“绘制”新的图片,这里先给出代码,再分析:
1
2
3
4
5
6
7
8
9
10
11
|
public static Bitmap crop( Bitmap bitmap, Rect cropRect ) {
return Bitmap.createBitmap(bitmap,cropRect.left,cropRect.top,cropRect.width(),cropRect.height());
} public static Bitmap cropWithCanvas( Bitmap bitmap, Rect cropRect ) {
Rect destRect = new Rect( 0 , 0 ,cropRect.width(),cropRect.height());
Bitmap cropped = Bitmap.createBitmap(cropRect.width(),cropRect.height(),Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(cropped);
canvas.drawBitmap(bitmap,cropRect,destRect, null );
return cropped;
} |
其实第一种方法内部实现也是利用了Canvas对象来“绘制”新的图片的,Canvas对象通过一个Bitmap对象来构建,该Bitmap即为“画布”,drawBitmap则是将源bitmap对象“画”到“画布”之中,这样就实现了数据的搬移,实现了图片的剪裁。
4. 旋转图片
Android中旋转图片同样是通过Bitmap的createBitmap方法来生成旋转后的图片,不过图片的旋转需要借助Matrix对象来协助完成,代码如下:
1
2
3
4
5
|
public static Bitmap rotate( Bitmap bitmap, int degrees ) {
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
return Bitmap.createBitmap(bitmap, 0 , 0 ,bitmap.getWidth(),bitmap.getHeight(),matrix, true );
} |
当然,图片的旋转也是可以通过Canvas来“绘制”,由于图片旋转会导致边界坐标发生变化,所以需要以图片中心点坐标为中心来旋转,具体实现见如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public static Bitmap rotateWithCanvas( Bitmap bitmap, int degrees ) {
int destWidth,destHeight;
float centerX = bitmap.getWidth()/ 2 ;
float centerY = bitmap.getHeight()/ 2 ;
// We want to do the rotation at origin, but since the bounding
// rectangle will be changed after rotation, so the delta values
// are based on old & new width/height respectively.
Matrix matrix = new Matrix();
matrix.preTranslate(-centerX,-centerY);
matrix.postRotate(degrees);
if ( degrees/ 90 % 2 == 0 ) {
destWidth = bitmap.getWidth();
destHeight = bitmap.getHeight();
matrix.postTranslate(centerX,centerY);
}
else {
destWidth = bitmap.getHeight();
destHeight = bitmap.getWidth();
matrix.postTranslate(centerY,centerX);
}
Bitmap cropped = Bitmap.createBitmap(destWidth,destHeight,Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(cropped);
canvas.drawBitmap(bitmap, matrix, null );
return cropped;
} |
本文转自 Jhuster 51CTO博客,原文链接:http://blog.51cto.com/ticktick/1604074,如需转载请自行联系原作者