Android开发实践:自己动手编写图片剪裁应用(3)
打开图片 图片的打开主要是把各种格式的图片转换为Bitmap对象,Android通过BitmapFactory类提供了一系列的静态方法来协助完成这个操作,如下所示: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class BitmapFactory{ public static BitmapdecodeFile(StringpathName,Optionsopts); public static BitmapdecodeFile(StringpathName); public static BitmapdecodeResourceStream(Resourcesres,TypedValuevalue, InputStreamis,Rectpad,Optionsopts); public static BitmapdecodeResource(Resourcesres, int id,Optionsopts); public static BitmapdecodeResource(Resourcesres, int id); public static BitmapdecodeByteArray( byte []data, int offset, int length,Optionsopts); public static BitmapdecodeByteArray( byte []data, int offset, int length); public static BitmapdecodeStream(InputStreamis,RectoutPadding,Optionsopts); public static BitmapdecodeStream(InputStreamis); public static BitmapdecodeFileDescriptor(FileDescriptorfd,RectoutPadding,Optionsopts); public static BitmapdecodeFileDescriptor(FileDescriptorfd); } 通过这些静态方法,我们可以方便地从文件、资源、字节流等各种途径打开图片,生成Bitmap对象。下面给出一个从文件中打开图片的函数封装: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static Bitmapload(Stringfilepath){ Bitmapbitmap= null ; try { FileInputStreamfin= new FileInputStream(filepath); bitmap=BitmapFactory.decodeStream(fin); fin.close(); } catch (FileNotFoundExceptione){ } catch (IOExceptione){ } return bitmap; } 2. 保存图片 图片的保存则主要通过Bitmap的compress方法,该方法的原型如下: 1 2 3 4 5 6 7 8 9 10 11 /** *Writeacompressedversionofthebitmaptothespecifiedoutputstream. *@paramformatTheformatofthecompressedimage *@paramqualityHinttothecompressor,0-100.0meaningcompressfor *smallsize,100meaningcompressformaxquality.Some *formats,likePNGwhichislossless,willignorethe *qualitysetting *@paramstreamTheoutputstreamtowritethecompresseddata. *@returntrueifsuccessfullycompressedtothespecifiedstream. */ public boolean compress(CompressFormatformat, int quality,OutputStreamstream) 第一个参数是图片格式,只有JPEG、PNG和WEBP三种,第二个参数是压缩质量(0~100),数值越大图片信息损失越小,第三个参数则是文件流对象。 同样,这里给出一个保存图片的函数封装: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public static void save(Bitmapbitmap,Stringfilepath){ try { FileOutputStreamfos= new FileOutputStream(filepath); bitmap.compress(CompressFormat.JPEG, 100 ,fos); bitmap.recycle(); fos.close(); } catch (FileNotFoundExceptione){ } catch (IOExceptione){ } } 3. 剪裁图片 Android中剪裁图片主要有2种方法,一种通过Bitmap的createBitmap方法来生成剪裁的图片,另一种则是通过Canvas对象来“绘制”新的图片,这里先给出代码,再分析: 1 2 3 4 5 6 7 8 9 10 11 public static Bitmapcrop(Bitmapbitmap,RectcropRect){ return Bitmap.createBitmap(bitmap,cropRect.left,cropRect.top,cropRect.width(),cropRect.height()); } public static BitmapcropWithCanvas(Bitmapbitmap,RectcropRect){ RectdestRect= new Rect( 0 , 0 ,cropRect.width(),cropRect.height()); Bitmapcropped=Bitmap.createBitmap(cropRect.width(),cropRect.height(),Bitmap.Config.RGB_565); Canvascanvas= 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 Bitmaprotate(Bitmapbitmap, int degrees){ Matrixmatrix= 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 BitmaprotateWithCanvas(Bitmapbitmap, int degrees){ int destWidth,destHeight; float centerX=bitmap.getWidth()/ 2 ; float centerY=bitmap.getHeight()/ 2 ; //Wewanttodotherotationatorigin,butsincethebounding //rectanglewillbechangedafterrotation,sothedeltavalues //arebasedonold&newwidth/heightrespectively. Matrixmatrix= 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); } Bitmapcropped=Bitmap.createBitmap(destWidth,destHeight,Bitmap.Config.RGB_565); Canvascanvas= new Canvas(cropped); canvas.drawBitmap(bitmap,matrix, null ); return cropped; } 本文转自 Jhuster 51CTO博客,原文链接:http://blog.51cto.com/ticktick/1604074,如需转载请自行联系原作者