android 动态裁剪图片,Android开发实践:自己动手编写图片剪裁应用(3)

前面两篇文章分别介绍了我编写的开源项目ImageCropper库,以及如何调用系统的图片剪裁模块,本文则继续分析一下开发Android图片剪裁应用中需要用到的Bitmap操作。

在Android系统中,对图片的操作主要是通过Bitmap类和Matrix类来完成,本文就介绍一下图片剪裁应用中对Bitmap的一些操作,包括:打开、保存、剪裁、旋转等,我已经将这些操作都封装到了一个BitmapHelper.java类中,放到GitHub上了(点击这里),大家可以方便地集成到自己的项目中。

打开图片

图片的打开主要是把各种格式的图片转换为Bitmap对象,Android通过BitmapFactory类提供了一系列的静态方法来协助完成这个操作,如下所示:

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对象。下面给出一个从文件中打开图片的函数封装:

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方法,该方法的原型如下:

/**

* Write a compressed version of the bitmap to the specified outputstream.

* @param format   The format of the compressed p_w_picpath

* @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),数值越大图片信息损失越小,第三个参数则是文件流对象。

同样,这里给出一个保存图片的函数封装:

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对象来“绘制”新的图片,这里先给出代码,再分析:

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对象来协助完成,代码如下:

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来“绘制”,由于图片旋转会导致边界坐标发生变化,所以需要以图片中心点坐标为中心来旋转,具体实现见如下代码:

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;

}

5. 小结

关于Bitmap的相关操作就介绍到这里了,更多的代码示例和实现可以参考我的开源项目ImageCropper,

该项目的GitHub地址:https://github.com/Jhuster/ImageCropper,有任何疑问欢迎留言讨论或者来信lujun.hust@gmail.com交流,或者关注我的新浪微博 @卢_俊 获取最新的文章和资讯。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值