android图片处理方法
- 格式:doc
- 大小:371.50 KB
- 文档页数:26
Android移动应用开发技术中的相机与图像处理方法随着智能手机的普及,移动应用开发成为了一个蓬勃发展的行业。
在这个行业中,相机和图像处理技术占据了重要的地位。
本文将探讨在Android移动应用开发中相机和图像处理方法的应用。
1. 相机的应用相机是手机最常用的功能之一,随时随地都可以记录下美好的瞬间。
在Android开发中,可以使用系统默认提供的相机应用,也可以自定义相机功能。
自定义相机可以为应用程序带来更多的功能和自由度。
例如,可以添加实时滤镜、调整曝光度、对焦方式等。
为了实现这些功能,需要了解Android相机API和图像处理技术。
2. 图像数据处理在Android移动应用开发中,我们需要对相机拍摄的图像数据进行处理。
有两种处理图像数据的方法:使用Bitmap类和使用OpenCV 库。
Bitmap类是Android提供的图像处理工具之一。
它可以用于图像的压缩、裁剪、缩放等基本操作。
同时,它还可以进行像素级的操作,如亮度调整、颜色滤镜应用等。
使用OpenCV库可以进行更为复杂的图像处理操作。
OpenCV是一个开源的计算机视觉库,提供了很多强大的图像处理算法和函数。
使用OpenCV可以实现人脸识别、图像分割、特征提取等高级图像处理任务。
3. 相机功能的扩展除了基本的拍照功能,相机还可以实现更多的功能扩展。
例如,人脸识别、拍摄连拍、HDR模式等等。
这些功能的实现需要深入了解Android相机API和图像处理算法。
人脸识别是一种常见的相机应用扩展功能。
通过分析图像中的人脸特征,可以实现面部表情检测、人脸识别等功能。
实现人脸识别需要使用OpenCV库中的人脸分类器和特征提取技术。
拍摄连拍是另一个常见的相机应用扩展功能。
通过一次性连续拍摄多张照片,可以捕捉到更多美丽瞬间。
实现拍摄连拍功能需要使用Android相机API中的连续拍摄模式。
HDR模式是拍摄高动态范围图像的一种技术。
通过在不同曝光条件下拍摄多张照片,然后将这些照片合成一张高质量的图像,实现更丰富的画面细节。
Android移动应用开发技术中的相机与图像处理方法近年来,随着智能手机的普及和移动应用的快速发展,Android 移动应用开发成为了互联网行业的热门领域。
在许多应用中,相机和图像处理是必不可少的功能模块。
本文将深入探讨Android移动应用开发中的相机与图像处理方法,介绍相关技术和应用案例。
一、相机技术在移动应用中的作用相机技术在移动应用中扮演着重要的角色。
通过调用相机功能,用户可以随时随地拍摄照片或录制视频,并将其应用到各种场景中。
例如,社交媒体应用中的照片分享、拍摄应用中的实时特效、安全应用中的人脸识别等等。
1. 拍照和录制视频Android提供了丰富的API和框架,使开发者可以轻松地调用相机功能。
借助Camera类和Camera2 API,开发者可以实现拍照和录制视频的功能。
他们可以控制相机的各种参数,如曝光、焦距、白平衡等,以获得满意的拍摄效果。
同时,Android还提供了MediaRecorder 类,让开发者能够方便地录制和处理视频。
2. 实时特效为了增加用户的拍摄乐趣和体验,许多应用都提供了各种实时特效功能。
比如,美颜相机应用可以通过对相片进行自动美颜处理,让用户在拍摄时就看到美丽的自己;滤镜相机应用则可以通过添加各种滤镜效果,为拍摄的照片或视频增添艺术感。
这些实时特效功能往往需要借助图像处理技术来实现。
二、图像处理技术在移动应用中的应用图像处理技术是相机应用中必不可少的一环。
它可以对拍摄的照片或视频进行各种处理和增强,提升用户的观感和使用体验。
1. 自动美化自动美化是相机应用中常见的一种图像处理技术。
通过利用人脸识别技术,应用可以检测照片中的人脸,并自动调整肤色、磨皮、瘦脸等,从而使用户在拍摄时就得到满意的效果。
2. 滤镜效果滤镜是图像处理中常见的一种技术。
应用可以通过添加各种滤镜效果,如黑白、复古、LOMO等,让拍摄的照片或视频呈现出不同的风格和色调。
3. 图像识别图像识别是近年来发展迅猛的一种技术。
Android移动应用开发技术中的相机与图像处理方法随着智能手机的普及,移动应用开发变得越来越重要。
在移动应用中,相机和图像处理是常见的功能模块,通过这些功能模块,用户可以拍照、编辑照片并与其他用户分享。
因此,熟悉Android移动应用开发技术中的相机与图像处理方法是每个开发者必备的基础知识。
首先,我们来探讨Android移动应用开发中的相机功能。
相机是移动应用中常见的一个重要功能,可以通过手机的摄像头拍摄照片或视频。
在Android开发中,通过使用Camera API可以实现相机功能。
通过调用Camera API的相关方法,开发者可以访问摄像头设备,打开摄像头预览界面,设置摄像头的参数等。
此外,开发者还可以在相机预览界面上添加特效和标签等元素,以增加用户的互动体验。
进一步,我们可以探讨Android移动应用开发中的图像处理方法。
图像处理是Android应用开发中另一个重要的技术,通过对图片进行处理,可以改变图片的外观和质量。
在Android开发中,图像处理可以通过使用Bitmap类来实现。
Bitmap是Android平台上的一个重要图像处理工具,开发者可以通过Bitmap类的相关方法对图片进行缩放、旋转、裁剪、滤镜和颜色调整等操作。
此外,开发者还可以使用第三方图像处理库来实现更复杂的图像处理功能,如OpenCV和Picasso等。
除了上述的基本功能,相机和图像处理在Android应用开发中还有更多的应用场景。
例如,许多社交媒体应用和照片编辑应用都使用相机和图像处理技术。
在社交媒体应用中,用户可以拍照并分享到朋友圈或发送给其他用户。
而在照片编辑应用中,用户可以对照片进行滤镜、修饰、合成和润色等处理,以创作出更具艺术性的照片。
此外,相机和图像处理技术还可以应用在安防领域,如人脸识别和图像识别等。
在实际开发中,相机和图像处理功能的实现并不简单。
开发者需要考虑到设备的兼容性、性能和用户体验等因素。
例如,在相机功能的实现中,开发者需要判断设备是否支持相机功能,处理相机权限和异常情况,以确保应用的稳定性和安全性。
Android中图片的处理(放大缩小,去色,转换格式,增加水印等)1package com.dzh.operateimage;2import android.graphics.Bitmap;3import android.graphics.Bitmap.Config;4import android.graphics.BitmapFactory;5import android.graphics.Canvas;6import android.graphics.ColorMatrix;7import android.graphics.ColorMatrixColorFilter;8import android.graphics.Paint;9import android.graphics.PorterDuff.Mode;10import android.graphics.PorterDuffXfermode;11import android.graphics.Rect;12import android.graphics.RectF;13import android.graphics.drawable.BitmapDrawable;14import android.graphics.drawable.Drawable;15import java.io.ByteArrayOutputStream;16import java.io.File;17import java.io.FileNotFoundException;18import java.io.FileOutputStream;19import java.io.IOException;20/**21* 处理图片的工具类.22*/23public class ImageTools {24public static final int LEFT = 0;25public static final int RIGHT = 1;26public static final int TOP = 3;27public static final int BOTTOM = 4;28/** */29/**30* 图片去色,返回灰度图片31*32* @param bmpOriginal 传入的图片33* @return 去色后的图片34*/35public static Bitmap toGrayscale(Bitmap bmpOriginal) {36int width, height;37height = bmpOriginal.getHeight();38width = bmpOriginal.getWidth();39Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 40Canvas c = new Canvas(bmpGrayscale);41Paint paint = new Paint();42ColorMatrix cm = new ColorMatrix();43cm.setSaturation(0);44ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);45paint.setColorFilter(f);46 c.drawBitmap(bmpOriginal, 0, 0, paint);47return bmpGrayscale;48}49/** */50/**51* 去色同时加圆角52*53* @param bmpOriginal 原图54* @param pixels 圆角弧度55* @return 修改后的图片56*/57public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) {58return toRoundCorner(toGrayscale(bmpOriginal), pixels);59}60/** */61/**62* 把图片变成圆角63*64* @param bitmap 需要修改的图片65* @param pixels 圆角的弧度66* @return 圆角图片67*/68public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {69Bitmap output = Bitmap70.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 71Canvas canvas = new Canvas(output);72final int color = 0xff424242;73final Paint paint = new Paint();74final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 75final RectF rectF = new RectF(rect);76final float roundPx = pixels;77paint.setAntiAlias(true);78canvas.drawARGB(0, 0, 0, 0);79paint.setColor(color);80canvas.drawRoundRect(rectF, roundPx, roundPx, paint);81paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));82canvas.drawBitmap(bitmap, rect, rect, paint);83return output;84}85/** */86/**87* 使圆角功能支持BitampDrawable88*89* @param bitmapDrawable90* @param pixels91* @return92*/93public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable, int pixels) { 94Bitmap bitmap = bitmapDrawable.getBitmap();95bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));96return bitmapDrawable;97}98/**99* 读取路径中的图片,然后将其转化为缩放后的bitmap100*101* @param path102*/103public static void saveBefore(String path) {104BitmapFactory.Options options = new BitmapFactory.Options();105options.inJustDecodeBounds = true;106// 获取这个图片的宽和高107Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回bm为空108options.inJustDecodeBounds = false;109// 计算缩放比110int be = (int)(options.outHeight / (float)200);111if (be <= 0)112be = 1;113options.inSampleSize = 2; // 图片长宽各缩小二分之一114// 重新读入图片,注意这次要把options.inJustDecodeBounds 设为false哦115bitmap = BitmapFactory.decodeFile(path, options);116int w = bitmap.getWidth();117int h = bitmap.getHeight();118System.out.println(w + " " + h);119// savePNG_After(bitmap,path);120saveJPGE_After(bitmap, path);121}122/**123* 保存图片为PNG124*125* @param bitmap126* @param name127*/128public static void savePNG_After(Bitmap bitmap, String name) {129File file = new File(name);130try {131FileOutputStream out = new FileOutputStream(file);132if (press(pressFormat.PNG, 100, out)) {133out.flush();134out.close();135}136} catch (FileNotFoundException e) {137 e.printStackTrace();138} catch (IOException e) {139 e.printStackTrace();140}141}142/**143* 保存图片为JPEG144*145* @param bitmap146* @param path147*/148public static void saveJPGE_After(Bitmap bitmap, String path) {149File file = new File(path);150try {151FileOutputStream out = new FileOutputStream(file);152if (press(pressFormat.JPEG, 100, out)) {153out.flush();154out.close();155}156} catch (FileNotFoundException e) {157 e.printStackTrace();158} catch (IOException e) {159 e.printStackTrace();160}161}162/**163* 水印164*165* @param bitmap166* @return167*/168public static Bitmap createBitmapForWatermark(Bitmap src, Bitmap watermark) { 169if (src == null) {170return null;171}172int w = src.getWidth();173int h = src.getHeight();174int ww = watermark.getWidth();175int wh = watermark.getHeight();176// create the new blank bitmap177Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图178Canvas cv = new Canvas(newb);179// draw src into180cv.drawBitmap(src, 0, 0, null);// 在0,0坐标开始画入src181// draw watermark into182cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印183// save all clip184cv.save(Canvas.ALL_SAVE_FLAG);// 保存185// store186cv.restore();// 存储187return newb;188}189/**190* 图片合成191*192* @return193*/194public static Bitmap potoMix(int direction, Bitmap... bitmaps) {195if (bitmaps.length <= 0) {196return null;197}198if (bitmaps.length == 1) {199return bitmaps[0];200}201Bitmap newBitmap = bitmaps[0];202// newBitmap = createBitmapForFotoMix(bitmaps[0],bitmaps[1],direction);203for (int i = 1; i < bitmaps.length; i++) {204newBitmap = createBitmapForFotoMix(newBitmap, bitmaps[i], direction);205}206return newBitmap;207}208209private static Bitmap createBitmapForFotoMix(Bitmap first, Bitmap second, int direction) {210if (first == null) {211return null;212}213if (second == null) {214return first;215}216int fw = first.getWidth();217int fh = first.getHeight();218int sw = second.getWidth();219int sh = second.getHeight();220Bitmap newBitmap = null;221if (direction == LEFT) {222newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Config.ARGB_8888); 223Canvas canvas = new Canvas(newBitmap);224canvas.drawBitmap(first, sw, 0, null);225canvas.drawBitmap(second, 0, 0, null);226} else if (direction == RIGHT) {227newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Config.ARGB_8888); 228Canvas canvas = new Canvas(newBitmap);229canvas.drawBitmap(first, 0, 0, null);230canvas.drawBitmap(second, fw, 0, null);231} else if (direction == TOP) {232newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Config.ARGB_8888); 233Canvas canvas = new Canvas(newBitmap);234canvas.drawBitmap(first, 0, sh, null);235canvas.drawBitmap(second, 0, 0, null);236} else if (direction == BOTTOM) {237newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Config.ARGB_8888); 238Canvas canvas = new Canvas(newBitmap);239canvas.drawBitmap(first, 0, 0, null);240canvas.drawBitmap(second, 0, fh, null);241}242return newBitmap;243}244/**245* 将Bitmap转换成指定大小246* @param bitmap247* @param width248* @param height249* @return250*/251public static Bitmap createBitmapBySize(Bitmap bitmap,int width,int height)252{253return Bitmap.createScaledBitmap(bitmap, width, height, true);254}255/**256* Drawable 转Bitmap257*258* @param drawable259* @return260*/261public static Bitmap drawableToBitmapByBD(Drawable drawable) {262BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;263return bitmapDrawable.getBitmap();264}265/**266* Bitmap 转Drawable267*268* @param bitmap269* @return270*/271public static Drawable bitmapToDrawableByBD(Bitmap bitmap) { 272Drawable drawable = new BitmapDrawable(bitmap);273return drawable;274}275/**276* byte[] 转bitmap277*278* @param b279* @return280*/281public static Bitmap bytesToBimap(byte[] b) {282if (b.length != 0) {283return BitmapFactory.decodeByteArray(b, 0, b.length);284} else {285return null;286}287}288/**289* bitmap 转byte[]290*291* @param bm292* @return293*/294public static byte[] bitmapToBytes(Bitmap bm) {295ByteArrayOutputStream baos = new ByteArrayOutputStream(); press(pressFormat.PNG, 100, baos);297return baos.toByteArray();298}299}。
Android图片处理(Matrix,ColorMatrix)在编程中有时候需要对图片做特殊的处理,比如将图片做出黑白的,或者老照片的效果,有时候还要对图片进行变换,以拉伸,扭曲等等。
这些效果在android中有很好的支持,通过颜色矩阵(C olorM atrix)和坐标变换矩阵(M atrix)可以完美的做出上面的所说的效果。
下面将分别介绍这两个矩阵的用法和相关的函数。
颜色矩阵android中可以通过颜色矩阵(ColorMatrix类)方面的操作颜色,颜色矩阵是一个5x4 的矩阵(如图1.1)可以用来方面的修改图片中RGBA各分量的值,颜色矩阵以一维数组的方式存储如下:[ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ]他通过RGBA四个通道来直接操作对应颜色,如果会使用Photoshop就会知道有时处理图片通过控制RGBA各颜色通道来做出特殊的效果。
这个矩阵对颜色的作用计算方式如1.3示:矩阵的运算规则是矩阵A的一行乘以矩阵C的一列作为矩阵R的一行,C矩阵是图片中包含的ARGB信息,R矩阵是用颜色矩阵应用于C之后的新的颜色分量,运算结果如下:R' = a*R + b*G + c*B + d*A + e;G' = f*R + g*G + h*B + i*A + j;B' = k*R + l*G + m*B + n*A + o;A' = p*R + q*G + r*B + s*A + t;颜色矩阵并不是看上去那么深奥,其实需要使用的参数很少,而且很有规律第一行决定红色第二行决定绿色第三行决定蓝色,第四行决定了透明度,第五列是颜色的偏移量。
下面是一个实际中使用的颜色矩阵。
如果把这个矩阵作用于各颜色分量的话,R=A*C,计算后会发现,各个颜色分量实际上没有任何的改变(R'=R G'=G B'=B A'=A)。
移动开发中,内存资源很宝贵,而且对加载图片内存空间也有限制;所以我们会在加载图片对图片进行相应的处理,有时为了提高响应速度,增强用户体验,我们在加载大图片时会先加载图片的缩略图、如后加载原图,所以我们要将图片按照固定大小取缩略图,一般取缩略图的方法是使用BitmapFactory的decodeFile方法,然后通过传递进去BitmapFactory.Option类型的参数进行取缩略图,在Option中,属性值inSampleSize表示缩略图大小为原始图片大小的几分之一,即如果这个值为2,则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。
然而,如果我们想取固定大小的缩略图就比较困难了,比如,我们想将不同大小的图片去出来的缩略图高度都为200px,而且要保证图片不失真,那怎么办?我们总不能将原始图片加载到内存中再进行缩放处理吧,要知道在移动开发中,内存是相当宝贵的,而且一张100K的图片,加载完所占用的内存何止 100K?经过研究,发现,Options中有个属性inJustDecodeBounds,研究了一下,终于明白是什么意思了,SDK中的E文是这么说的If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.意思就是说如果该值设为true那么将不返回实际的bitmap不给其分配内存空间而里面只包括一些解码边界信息即图片大小信息,那么相应的方法也就出来了,通过设置inJustDecodeBounds为true,获取到outHeight(图片原始高度)和 outWidth(图片的原始宽度),然后计算一个inSampleSize(缩放值),然后就可以取图片了,这里要注意的是,inSampleSize 可能小于0,必须做判断。
Android图形图像处理实践随着智能手机的普及以及人们对图像的需求日益增加,图形图像处理技术这一领域也愈发重要。
Android作为目前最流行的移动操作系统之一,其开放性和易用性吸引了越来越多的开发者和用户。
在Android开发中,图形图像处理是一项非常重要的任务。
本文将介绍在Android平台上进行图形图像处理的实践。
一、Bitmap类在Android平台上,Bitmap类是进行图像处理的主要工具。
它提供了大量的方法来进行图像处理,例如压缩和缩放等等。
Bitmap类是Android平台上非常重要的类之一,它在内存中保存图像数据,我们可以对它进行许多操作,例如绘制、裁剪、旋转和色彩处理等等。
二、图像压缩由于手机中的内存和存储容量受限,所以我们需要对图片进行压缩处理。
常见的压缩方法是通过设置Bitmap类中的options参数来实现。
options 中有一个inSampleSize参数,用于指定采样率,即裁剪图片时选取的点数。
我们可以通过设置inSampleSize的值来实现压缩,这也是Android平台上最常用的压缩方法之一。
三、图像缩放在Android平台上,图像缩放也是需要用到Bitmap类的。
由于实际应用中需要缩放的图片大小和比例各不相同,所以我们需要提供不同的缩放方法。
其中最常用的方法是通过设置Bitmap类的options参数来实现缩放。
四、图像裁剪在进行图像处理时,图像裁剪也是非常常见的操作。
在Android平台上,我们可以通过设置Bitmap类的裁剪区域来实现裁剪,进而实现对图片的处理。
五、图像色彩处理当我们在进行图像处理时,有时候需要修改图片的色彩。
在Android平台上,我们可以通过设置Bitmap类的色彩矩阵来实现色彩处理。
色彩矩阵是一个4x5的矩阵,它定义了一个变换矩阵,可以通过变换矩阵来改变图片的颜色和透明度等属性。
六、图像旋转进行图像旋转也是非常常见的操作之一。
在Android平台上,我们可以通过设置Bitmap类的旋转角度来实现图像旋转。
Android处理图像数据转换的各种⽅法Android中处理图像是⼀件很常见的事情,这⾥记录备忘⼀些亲⾝使⽤过的处理图⽚数据的⽅法。
转为BitmapRGB值转Bitmap复制代码代码如下:private Bitmap createColorBitmap(String rgb, int width, int height) {Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);int color = Color.parseColor(rgb);bmp.eraseColor(color);return bmp;}//UsageBitmap bmp = createColorBitmap("#cce8cf", 200, 50);Color值转Bitmap复制代码代码如下:private Bitmap createColorBitmap(int color, int width, int height) {Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);bmp.eraseColor(color);return bmp;}//UsageBitmap bmp = createColorBitmap(Color.BLUE, 200, 50);字节数组转Bitmap复制代码代码如下:private Bitmap getBitmapFromByteArray(byte[] array) {return BitmapFactory.decodeByteArray(array, 0, array.length);}读取⽂件转Bitmap复制代码代码如下:private Bitmap getBitmapFromFile(String pathName) {return BitmapFactory.decodeFile(pathName);}读取资源转Bitmap复制代码代码如下:private Bitmap getBitmapFromResource(Resources res, int resId) {return BitmapFactory.decodeResource(res, resId);}输⼊流转Bitmap复制代码代码如下:private Bitmap getBitmapFromStream(InputStream inputStream) {return BitmapFactory.decodeStream(inputStream);}Drawable转Bitmap复制代码代码如下:Bitmap icon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);转为Drawable资源转Drawable复制代码代码如下:Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);Bitmap转Drawable复制代码代码如下:Drawable d = new BitmapDrawable(getResources(),bitmap);图⽚圆⾓展⽰通过对图⽚数据bitmap进⾏处理即可,其中pixels为边⾓的半径。
Android图片处理工具类(圆角,压缩)工作中用到的图片处理工具类,简单写下来,以便备用!public class BitmapUtils {/*** 图像背景圆角处理* bitmap要处理的图片 roundPx 图片弯角的圆度一般是5到10之间*/public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {// 创建与原图大小一样的bitmap文件,Config.ARGB_8888根据情况可以改用其它的Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);// 实例画布,绘制的bitmap将保存至output中Canvas canvas = new Canvas(output);final int color = 0xff424242;//写自己需要的颜色值final Paint paint = new Paint();final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());final RectF rectF = new RectF(rect);paint.setAntiAlias(true);canvas.drawARGB(0, 0, 0, 0);paint.setColor(color);canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(newPorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint);bitmap.recycle();bitmap = null;return output;}/*** bitmap缩放* width要缩放的宽度 height要缩放的高度*/public static Bitmap getBitmapDeflation(Bitmap bitmap, int width, int height, boolean recycle) {if (null == bitmap) {return null;}float scaleWidth = 0f;float scaleHeight = 0f;// 获取bitmap宽高int bitmapWidth = bitmap.getWidth();int bitmapHeight = bitmap.getHeight();// 计算缩放比,图片的宽高小于指定的宽高则不缩放if (width < bitmapWidth) {scaleWidth = ((float) width) / bitmapWidth;} else {scaleWidth = 1.00f;}if (height < bitmapHeight) {scaleHeight = ((float) height) / bitmapHeight; } else {scaleHeight = 1.00f;}Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);if (recycle && !bitmap.isRecycled()) {bitmap.recycle();}bitmap = null;return newBitmap;}/**** 方法概述:进入图片的大小与质量压缩,用于区分大小图片*/public static Bitmap getCompressedImage(String srcPath) {BitmapFactory.Options newOpts = new BitmapFactory.Options();// 开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inPreferredConfig = Bitmap.Config.RGB_565;newOpts.inPurgeable = true;newOpts.inJustDecodeBounds = true;FileInputStream is = null;try {is = new FileInputStream(srcPath);} catch (FileNotFoundException e) {e.printStackTrace();}Bitmap bitmap = BitmapFactory.decodeStream(is, null, newOpts);// 此时返回bm为空newOpts.inJustDecodeBounds = false;int w = newOpts.outWidth;int h = newOpts.outHeight;// 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为float hh = 130f;// 这里设置高度为800ffloat ww = 130f;// 这里设置宽度为480f// 缩放比。
Android开发中的图像处理和计算机视觉技术近年来,随着智能手机的普及以及移动互联网的快速发展,Android开发已经成为了一个热门的领域。
在Android开发中,图像处理和计算机视觉技术扮演着非常重要的角色。
本文将探讨Android开发中的图像处理和计算机视觉技术。
第一部分:图像处理技术Android开发中的图像处理技术是指通过编程对图像进行操作和处理的一系列技术。
在Android开发中,图像处理技术广泛应用于各个领域,例如相机应用、图像编辑应用等。
首先,Android开发者可以利用图像处理技术实现相机应用的开发。
通过调用相机API,开发者可以实现拍照、录像等功能,并对拍摄的照片进行处理。
例如,可以实现自动对焦、曝光调节等功能,提升用户体验。
其次,图像处理技术在图像编辑应用中也扮演着重要的角色。
开发者可以通过使用滤镜技术、美颜算法等对图像进行处理,使用户可以轻松地对照片进行编辑。
这些功能通过图像处理技术的应用可以实现,为用户提供了更多的图像编辑选择。
此外,图像处理技术在图像识别应用中也起到了关键作用。
开发者可以利用机器学习算法、神经网络等技术,实现对图像中物体的识别和分类。
这不仅为用户提供了便利,也为更多的应用场景提供了可能性,例如人脸识别、场景识别等。
第二部分:计算机视觉技术计算机视觉技术是指通过计算机和摄像机等设备实现对图像和视频的理解和分析的技术。
在Android开发中,计算机视觉技术广泛应用于人机交互、安防监控等方面。
首先,计算机视觉技术在人机交互中起到了重要的作用。
开发者可以利用人脸识别技术实现人脸解锁、表情分析等功能。
这些功能不仅提升了用户的使用体验,也增加了系统的安全性。
其次,计算机视觉技术在安防监控方面也有着广泛的应用。
开发者可以利用运动检测技术、物体跟踪技术等实现对监控区域的自动监测。
通过实时分析视频中的内容,系统可以及时发现异常情况,并提醒用户或进行报警处理。
此外,计算机视觉技术还被应用于虚拟现实和增强现实领域。
将一个图片切割成多个图片有种场景,我们想将一个图片切割成多个图片。
比如我们在开发一个拼图的游戏,就首先要对图片进行切割。
以下是封装好的两个类,可以实现图片的切割。
仅供参考和学习。
一个是ImagePiece类,此类保存了一个Bitmap对象和一个标识图片的顺序索引的int变量。
Java代码1.import android.graphics.Bitmap;2.public class ImagePiece {3.public int index = 0;4.public Bitmap bitmap = null;5.}一个是ImageSplitter类,有一个静态方法split,传入的参数是要切割的Bitmap 对象,和横向和竖向的切割片数。
比如传入的是3、3,则横竖向都切割成3片,最终会将整个图片切割成3X3=9片。
Java代码1.import java.util.ArrayList;2.import java.util.List;3.4.import android.graphics.Bitmap;5.6.public class ImageSplitter {7.8.public static List<ImagePiece> split(Bitmap bitmap,int xPiece, int yPiece) {9.10. List<ImagePiece> pieces = new ArrayList<ImagePiece>(xPiece * yPiece);11. int width = bitmap.getWidth();12. int height = bitmap.getHeight();13. int pieceWidth = width / 3;14. int pieceHeight = height / 3;15. for (int i = 0; i < yPiece; i++) {16. for (int j = 0; j < xPiece; j++) {17. ImagePiece piece = new ImagePiece();18. piece.index = j + i * xPiece;19. int xValue = j * pieceWidth;20. int yValue = i * pieceHeight;21. piece.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue,22. pieceWidth, pieceHeight);23. pieces.add(piece);24. }25. }26.27. return pieces;28. }29.30.}1、图标加灰色过滤;2、android的图片资源默认是静态的,单实例;如果两个IM好友的头像一样,最简单的都是用的软件自带头像,有一个在线,一个离线,直接改变头像的灰度,则两个用户的头像都会变灰或者在线,答案是:Drawable.mutate()。
Java代码1.Drawable mDrawable = context.getResources().getDrawable(R.drawable.face_icon);2.//Make this drawable mutable.3.//A mutable drawable is guaranteed to not share its state with any other drawable.4.mDrawable.mutate();5.ColorMatrix cm = new ColorMatrix();6.cm.setSaturation(0);7.ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);8.mDrawable.setColorFilter(cf);生成缩略图,抠自android launcher源码:Java代码1./*2.* Copyright (C) 2008 The Android Open Source Project3.*4.* Licensed under the Apache License, Version 2.0 (the"License");5.* you may not use this file except in compliance with the License.6.* You may obtain a copy of the License at7.*8.* /licenses/LICENSE-2.09.*10. * Unless required by applicable law or agreed to inwriting, software11. * distributed under the License is distributed on an"AS IS" BASIS,12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13. * See the License for the specific language governingpermissions and14. * limitations under the License.15. */16.17.package uncher;18.19.import android.graphics.drawable.BitmapDrawable;20.import android.graphics.drawable.Drawable;21.import android.graphics.drawable.PaintDrawable;22.import android.graphics.Bitmap;23.import android.graphics.PixelFormat;24.import android.graphics.Canvas;25.import android.graphics.PaintFlagsDrawFilter;26.import android.graphics.Paint;27.import android.graphics.Rect;28.import android.content.res.Resources;29.import android.content.Context;30.31./**32. * Various utilities shared amongst the Launcher's classes.33. */34.final class Utilities {35. private static int sIconWidth = -1;36. private static int sIconHeight = -1;37.38. private static final Paint sPaint = new Paint();39. private static final Rect sBounds = new Rect();40. private static final Rect sOldBounds = new Rect();41. private static Canvas sCanvas = new Canvas();42.43. static {44. sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,45. Paint.FILTER_BITMAP_FLAG));46. }47.48. /**49. * Returns a Drawable representing the thumbnailof the specified Drawable.50. * The size of the thumbnail is defined by the dimension51. * uncher_application_icon_size.52. *53. * This method is not thread-safe and should be invoked on the UI thread only.54. *55. * @param icon The icon to get a thumbnail of.56. * @param context The application's context.57. *58. * @return A thumbnail for the specified iconor the icon itself if the59. * thumbnail could not be created.60. */61. static Drawable createIconThumbnail(Drawable icon,Context context) {62. if (sIconWidth == -1) {63. final Resources resources = context.getResources();64. sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);65. }66.67. int width = sIconWidth;68. int height = sIconHeight;69.70. float scale = 1.0f;71. if (icon instanceof PaintDrawable) {72. PaintDrawable painter = (PaintDrawable) icon;73. painter.setIntrinsicWidth(width);74. painter.setIntrinsicHeight(height);75. } else if (icon instanceof BitmapDrawable){76. // Ensure the bitmap has a density.77. BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;78. Bitmap bitmap = bitmapDrawable.getBitmap();79. if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {80. bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());81. }82. }83. int iconWidth = icon.getIntrinsicWidth();84. int iconHeight = icon.getIntrinsicHeight();85.86. if (width > 0&& height > 0) {87. if (width < iconWidth || height <iconHeight || scale != 1.0f) {88. final float ratio = (float)iconWidth / iconHeight;89.90. if (iconWidth > iconHeight){91. height = (int) (width / ratio);92. } else if (iconHeight > iconWidth) {93. width = (int) (height * ratio);94. }95.96. final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?97. Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;98. final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);99. final Canvas canvas = sCanvas;100.canvas.setBitmap(thumb);101.// Copy the old bounds to restore them later102.// If we were to do oldBounds = icon.getBounds(),103.// the call to setBoun ds() that follows would104.// change the same ins tance and we would lose the105.// old bounds106.sOldBounds.set(icon.getBou nds());107.final int x = (sIconWi dth - width) / 2;108.final int y = (sIconHe ight - height) / 2;109.icon.setBounds(x, y, x + width, y + height);110.icon.draw(canvas);111.icon.setBounds(sOldBounds) ;112.icon = new FastBitmapDr awable(thumb);113.} else if (iconWidth < width && iconHeight < height) {114.final Bitmap.Config c = Bitmap.Config.ARGB_8888;115.final Bitmap thumb = B itmap.createBitmap(sIconWidth, sIconHeight, c);116.final Canvas canvas = sCanvas;117.canvas.setBitmap(thumb);118.sOldBounds.set(icon.getBou nds());119.final int x = (width - iconWidth) / 2;120.final int y = (height - iconHeight) / 2;121.icon.setBounds(x, y, x + iconWidth, y + iconHeight);122.icon.draw(canvas);123.icon.setBounds(sOldBounds) ;124.icon = new FastBitmapDr awable(thumb);125.}126.}127.128.return icon;129.}130.131./**132.* Returns a Bitmap representing the thumbn ail of the specified Bitmap.133.* The size of the thumbnail is defined b y the dimension134.* uncher_application_icon_size.135.*136.* This method is not thread-safe and shou ld be invoked on the UI thread only.137.*138.* @param bitmap The bitmap to get a thum bnail of.139.* @param context The application's context.140.*141.* @return A thumbnail for the specified b itmap or the bitmap itself if the142.* thumbnail could not be cr eated.143.*/144.static Bitmap createBitmapThumbnail(Bitmap bitm ap, Context context) {145.if (sIconWidth == -1) {146.final Resources resources = co ntext.getResources();147.sIconWidth = sIconHeight = (in t) resources.getDimension(148.android.R.dimen.ap p_icon_size);149.}150.151.int width = sIconWidth;152.int height = sIconHeight;153.154.final int bitmapWidth = bitmap.getWidt h();155.final int bitmapHeight = bitmap.getHei ght();156.157.if (width > 0&& height > 0) {158.if (width < bitmapWidth || he ight < bitmapHeight) {159.final float ratio = (f loat) bitmapWidth / bitmapHeight;160.161.if (bitmapWidth > bitma pHeight) {162.height = (int) (width / ratio);163.} else if (bitmapHeight > bitmapWidth) {164.width = (int) ( height * ratio);165.}166.167.final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ? 168.bitmap.get Config() : Bitmap.Config.ARGB_8888;169.final Bitmap thumb = B itmap.createBitmap(sIconWidth, sIconHeight, c);170.final Canvas canvas = sCanvas;171.final Paint paint = sP aint;172.canvas.setBitmap(thumb);173.paint.setDither(false);174.paint.setFilterBitmap(true );175.sBounds.set((sIconWidth -width) / 2, (sIconHeight - height) / 2, width, heigh t);176.sOldBounds.set(0, 0, bit mapWidth, bitmapHeight);177.canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);178.return thumb;179.} else if (bitmapWidth < width || bitmapHeight < height) {180.final Bitmap.Config c = Bitmap.Config.ARGB_8888;181.final Bitmap thumb = B itmap.createBitmap(sIconWidth, sIconHeight, c);182.final Canvas canvas = sCanvas;183.final Paint paint = sP aint;184.canvas.setBitmap(thumb);185.paint.setDither(false);186.paint.setFilterBitmap(true );187.canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2,188.(sIconHeig ht - bitmapHeight) / 2, paint);189.return thumb;190.}191.}192.193.return bitmap;194.}195.}Java代码1.//Android Matrix类实现镜像方法2.public void drawRegion(Image image_src,3.4.int x_src, int y_src,5.6.int width, int height,7.8.int transform,9.10.int x_dest, int y_dest,11.12.int anchor){13.14.if((anchor&VCENTER) != 0){15.16.y_dest -= height/2;17.18.}else if((anchor&BOTTOM) != 0){19.20.y_dest -= height;21.22.}23.24.if((anchor&RIGHT) != 0){25.26.x_dest -= width;27.28.}else if((anchor&HCENTER) != 0){29.30.x_dest -= width/2;31.32.}33.34.Bitmap newMap = Bitmap.createBitmap(image_src.getBitmap(),x_src, y_src, width, height);35.36.Matrix mMatrix = new Matrix();37.38.Matrix temp = new Matrix();39.40.Matrix temp2 = new Matrix();41.42.float[] mirrorY = {43.44.-1, 0, 0,45.0, 1, 0,46.0, 0, 147.48.};49.50.temp.setValues(mirrorY);51.52.switch(transform){53.54.case Sprite.TRANS_NONE:55.56.break;57.58.case Sprite.TRANS_ROT90:59.60.mMatrix.setRotate(90,width/2, height/2);61.62.break;63.64.case Sprite.TRANS_ROT180:65.66.mMatrix.setRotate(180,width/2, height/2);67.68.break;69.70.case Sprite.TRANS_ROT270:71.72.mMatrix.setRotate(270,width/2, height/2);73.74.break;75.76.case Sprite.TRANS_MIRROR:77.78.mMatrix.postConcat(temp);79.80.break;81.82.case Sprite.TRANS_MIRROR_ROT90:83.84.mMatrix.postConcat(temp);85.86.mMatrix.setRotate(90,width/2, height/2);87.88.break;89.90.case Sprite.TRANS_MIRROR_ROT180:91.92.mMatrix.postConcat(temp);93.94.mMatrix.setRotate(180,width/2, height/2);95.96.break;97.98.case Sprite.TRANS_MIRROR_ROT270:99.100.mMatrix.postConcat(temp);101.102.mMatrix.setRotate(270,width/2, height/2);103.104.break;105.106.}107.108.mMatrix.setTranslate(x_dest, y_dest);109.110.canvas.drawBitmap(newMap, mMatrix, mPaint);111.112.}Java代码1.//图片Url保存为位图并进行缩放操作2.//通过传入图片url获取位图方法3.public Bitmap returnBitMap(String url) {4.URL myFileUrl = null;5.Bitmap bitmap = null;6.try {7.myFileUrl = new URL(url);8.} catch (MalformedURLException e) {9. e.printStackTrace();10. }11. try {12. HttpURLConnection conn = (HttpURLConnection) myFileUrl13. .openConnection();14. conn.setDoInput(true);15. conn.connect();16. InputStream is = conn.getInputStream();17. bitmap = BitmapFactory.decodeStream(is);18. is.close();19. } catch (IOException e) {20. e.printStackTrace();21. }22. Log.v(tag, bitmap.toString());23.24. return bitmap;25. }26.//通过传入位图,新的宽.高比进行位图的缩放操作27.public static Drawable resizeImage(Bitmap bitmap, int w,int h) {28.29. // load the origial Bitmap30. Bitmap BitmapOrg = bitmap;31.32. int width = BitmapOrg.getWidth();33. int height = BitmapOrg.getHeight();34. int newWidth = w;35. int newHeight = h;36.37. Log.v(tag, String.valueOf(width));38. Log.v(tag, String.valueOf(height));39.40. Log.v(tag, String.valueOf(newWidth));41. Log.v(tag, String.valueOf(newHeight));42.43. // calculate the scale44. float scaleWidth = ((float) newWidth) / width;45. float scaleHeight = ((float) newHeight) /height;46.47. // create a matrix for the manipulation48. Matrix matrix = new Matrix();49. // resize the Bitmap50. matrix.postScale(scaleWidth, scaleHeight);51. // if you want to rotate the Bitmap52. // matrix.postRotate(45);53.54. // recreate the new Bitmap55. Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,56. height, matrix, true);57.58. // make a Drawable from Bitmap to allowto set the Bitmap59. // to the ImageView, ImageButton or whatever60. return new BitmapDrawable(resizedBitmap);61.62. }Java代码1.1.图片加载方法,方便用户加载图片2./***3.* 加载本地图片4.* @param context:主运行函数实例5.* @param bitAdress:图片地址,一般指向R下的drawable目录6.* @return7.*/8.public final Bitmap CreatImage(Context context, int bitAdress) {9.Bitmap bitmaptemp = null;10.bitmaptemp = BitmapFactory.decodeResource(context.getResources(),11.bitAdress);12.return bitmaptemp;13.}14.2.图片平均分割方法,将大图平均分割为N行N列,方便用户使用15./***16.* 图片分割17.*18.* @param g19.* :画布20.* @param paint21.* :画笔22.* @param imgBit23.* :图片24.* @param x25.* :X轴起点坐标26.* @param y27.* :Y轴起点坐标28.* @param w29.* :单一图片的宽度30.* @param h31.* :单一图片的高度32.* @param line33.* :第几列34.* @param row35.* :第几行36.*/37.public final void cuteImage(Canvas g, Paint paint, Bitmap imgBit, int x,38.int y, int w, int h, int line, int row) {39.g.clipRect(x, y, x + w, h + y);40.g.drawBitmap(imgBit, x –line * w, y –row * h, paint);41.g.restore();42.}43.3.图片缩放,对当前图片进行缩放处理44./***45.* 图片的缩放方法46.*47.* @param bgimage48.* :源图片资源49.* @param newWidth50.* :缩放后宽度51.* @param newHeight52.* :缩放后高度53.* @return54.*/55.public Bitmap zoomImage(Bitmap bgimage, int newWidth, intnewHeight) {56.// 获取这个图片的宽和高57.int width = bgimage.getWidth();58.int height = bgimage.getHeight();59.// 创建操作图片用的matrix对象60.Matrix matrix = new Matrix();61.// 计算缩放率,新尺寸除原始尺寸62.float scaleWidth = ((float) newWidth) / width;63.float scaleHeight = ((float) newHeight) / height;64.// 缩放图片动作65.matrix.postScale(scaleWidth, scaleHeight);66.Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width,height,67.matrix, true);68.return bitmap;69.}70.4.绘制带有边框的文字,一般在游戏中起文字的美化作用71./***72.* 绘制带有边框的文字73.*74.* @param strMsg75.* :绘制内容76.* @param g77.* :画布78.* @param paint79.* :画笔80.* @param setx81.* ::X轴起始坐标82.* @param sety83.* :Y轴的起始坐标84.* @param fg85.* :前景色86.* @param bg87.* :背景色88.*/89.public void drawText(String strMsg, Canvas g, Paint paint, int setx,90.int sety, int fg, int bg) {91.paint.setColor(bg);92.g.drawText(strMsg, setx + 1, sety, paint);93.g.drawText(strMsg, setx, sety –1, paint);94.g.drawText(strMsg, setx, sety + 1, paint);95.g.drawText(strMsg, setx –1, sety, paint);96.paint.setColor(fg);97.g.drawText(strMsg, setx, sety, paint);98.g.restore();99.}100.5.Android 图片透明度处理代码101./**102.* 图片透明度处理103.*104.* @param sourceImg105.* 原始图片106.* @param number107.* 透明度108.* @return109.*/110.public static Bitmap setAlpha(Bitmap sourceImg, int number) {111.int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];112.sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0,sourceImg.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值113.number = number * 255/ 100;114.for (int i = 0; i < argb.length; i++) {115.argb = (number << 24) | (argb & 0×00FFFFFF);//修改最高2位的值116.}117.sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidt h(), sourceImg.getHeight(), Config.ARGB_8888);118.return sourceImg;119.}120.6.图片翻转121.Resources res = this.getContext().getResources();122.img = BitmapFactory.decodeResource(res, R.drawable.slog o);123.Matrix matrix = new Matrix();124.matrix.postRotate(90); /*翻转90度*/125.int width = img.getWidth();126.int height = img.getHeight();127.r_img = Bitmap.createBitmap(img, 0, 0, width, height , matrix, true);Java代码1.import android.graphics.Bitmap;2.import android.graphics.Canvas;3.import android.graphics.LinearGradient;4.import android.graphics.Matrix;5.import android.graphics.Paint;6.import android.graphics.PixelFormat;7.import android.graphics.PorterDuffXfermode;8.import android.graphics.Rect;9.import android.graphics.RectF;10.import android.graphics.Bitmap.Config;11.import android.graphics.PorterDuff.Mode;12.import android.graphics.Shader.TileMode;13.import android.graphics.drawable.Drawable;14./**15.*16.* @author superdev17.* @version 1.018.*19.*/20.public class ImageUtil {21.22./**23.* 放大缩小图片24.*/25.public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {26. int width = bitmap.getWidth();27. int height = bitmap.getHeight();28. Matrix matrix = new Matrix();29. float scaleWidht = ((float) w / width);30. float scaleHeight = ((float) h / height);31. matrix.postScale(scaleWidht, scaleHeight);32. Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);33. return newbmp;34.}35.36./**37.* 将Drawable转化为Bitmap38.*/39.public static Bitmap drawableToBitmap(Drawable drawable) {40. int width = drawable.getIntrinsicWidth();41. int height = drawable.getIntrinsicHeight();42. Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);43. Canvas canvas = new Canvas(bitmap);44. drawable.setBounds(0, 0, width, height);45. drawable.draw(canvas);46. return bitmap;47.48.}49.50./**51.* 获得圆角图片的方法53.public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx) {54.55. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Config.ARGB_8888);56. Canvas canvas = new Canvas(output);57.58. final int color = 0xff424242;59. final Paint paint = new Paint();60. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());61. final RectF rectF = new RectF(rect);62.63. paint.setAntiAlias(true);64. canvas.drawARGB(0, 0, 0, 0);65. paint.setColor(color);66. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);67.68. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));69. canvas.drawBitmap(bitmap, rect, rect, paint);70.71. return output;72.}73.74./**75.* 获得带倒影的图片方法76.*/77.public static Bitmap createReflectionImageWithOrigin(Bitmapbitmap) {78. final int reflectionGap = 4;79. int width = bitmap.getWidth();80. int height = bitmap.getHeight();81.82. Matrix matrix = new Matrix();83. matrix.preScale(1, -1);84.85. Bitmap reflectionImage = Bitmap.createBitmap(bitmap,0, height / 2, width, height / 2, matrix, false);86.87. Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);89. Canvas canvas = new Canvas(bitmapWithReflection);90. canvas.drawBitmap(bitmap, 0, 0, null);91. Paint deafalutPaint = new Paint();92. canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);93.94. canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);95.96. Paint paint = new Paint();97. LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflect ionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);98. paint.setShader(shader);99. // Set the Transfer mode to be porter duff anddestination in100.paint.setXfermode(new PorterDuffXfermode(Mode.DST_I N));101.// Draw a rectangle using the paint with our linear gradient102.canvas.drawRect(0, height, width, bitmapWithRefle ction.getHeight() + reflectionGap, paint);103.return bitmapWithReflection;104.}105.}Java代码1.private byte[] Bitmap2Bytes(Bitmap bm){2.ByteArrayOutputStream baos = new ByteArrayOutputStream();press(pressFormat.PNG, 100, baos);4.return baos.toByteArray();5.}6.private Bitmap Bytes2Bimap(byte[] b){7.if(b.length!=0){8.return BitmapFactory.decodeByteArray(b, 0, b.length);9.}10. else {11. return null;12. }13. }14.15. /**16. * create the bitmap from a byte array17. *生成水印图片18. * @param src the bitmap object you want proecss19. * @param watermark the water mark above the src20. * @return return a bitmap object ,if paramter's length is 0,return null21. */22. private Bitmap createBitmap( Bitmap src, Bitmap watermark )23. {24. String tag = "createBitmap";25. Log.d( tag, "create a new bitmap" );26. if( src == null )27. {28. return null;29. }30.31. int w = src.getWidth();32. int h = src.getHeight();33. int ww = watermark.getWidth();34. int wh = watermark.getHeight();35. //create the new blank bitmap36. Bitmap newb = Bitmap.createBitmap( w, h,Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图37. Canvas cv = new Canvas( newb );38. //draw src into39. cv.drawBitmap( src, 0, 0, null );//在0,0坐标开始画入src40. //draw watermark into41. cv.drawBitmap( watermark, w - ww + 5, h- wh + 5, null );//在src的右下角画入水印42. //save all clip43. cv.save( Canvas.ALL_SAVE_FLAG );//保存44. //store45. cv.restore();//存储46. return newb;47. }48. /** 重新编码Bitmap49. *50. * @param src51. * 需要重新编码的Bitmap52. *53. * @param format54. * 编码后的格式(目前只支持png和jpeg这两种格式)55. *56. * @param quality57. * 重新生成后的bitmap的质量58. *59. * @return60. * 返回重新生成后的bitmap61. */62. private static Bitmap codec(Bitmap src, pressFormat format,63.int quality) {64. ByteArrayOutputStream os = new ByteArrayOutputStream();65. press(format, quality, os);66.67. byte[] array = os.toByteArray();68. return BitmapFactory.decodeByteArray(array, 0, array.length);69. }70.71.//Stream转换成Byte72.static byte[] streamToBytes(InputStream is) {73. ByteArrayOutputStream os = new ByteArrayOutputStream(1024);74. byte[] buffer = new byte[1024];75. int len;76. try {77. while ((len = is.read(buffer)) >=0) {78. os.write(buffer, 0, len);79. }80. } catch (java.io.IOException e) {81.82. }83. return os.toByteArray();85.//把View转换成Bitmap86.87. /**88. * 把一个View的对象转换成bitmap89. */90. static Bitmap getViewBitmap(View v) {91.92. v.clearFocus();93. v.setPressed(false);94.95. //能画缓存就返回false96. boolean willNotCache = v.willNotCacheDrawing();97. v.setWillNotCacheDrawing(false);98. int color = v.getDrawingCacheBackgroundColor();99. v.setDrawingCacheBackgroundColor(0);100.if (color != 0) {101.v.destroyDrawingCache();102.}103.v.buildDrawingCache();104.Bitmap cacheBitmap = v.getDrawingCache( );105.if (cacheBitmap == null) {106.Log.e(TAG, "failed getViewBitmap (" + v + ")", new RuntimeException());107.return null;108.}109.Bitmap bitmap = Bitmap.createBitmap(cac heBitmap);110.// Restore the view111.v.destroyDrawingCache();112.v.setWillNotCacheDrawing(willNotCache);113.v.setDrawingCacheBackgroundColor(color);114.return bitmap;115.}Java代码1.读取raw资源文件中的mp3文件,然后通过音乐播放器播放:3./**4.* 把mp3文件写入卡5.*6.* @param fileName7.* 输出的文件名(全路径)8.* @param context9.* context对象10. */11. private void writeMP3ToSDcard(String fileName, Context context) {12. byte[] buffer = new byte[1024* 8];13. int read;14. BufferedInputStream bin = new BufferedInputStream(context.getResources().openRawResource(R.raw.ring));15. try {16. BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(fileName));17. while ((read = bin.read(buffer)) >-1) {18. bout.write(buffer, 0, read);19. }20. bout.flush();21. bout.close();22. bin.close();23. } catch (FileNotFoundException e) {24. e.printStackTrace();25. } catch (IOException e) {26. e.printStackTrace();27. }28. }29.30.31.Intent intent = new Intent();32.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);33.intent.setAction(android.content.Intent.ACTION_VIEW);34.intent.setDataAndType(Uri.fromFile(newFile("XXXXmp3的文件全路径")),"audio/*");35.startActivity(intent);绘制图像倒影Java代码1.private void2._Init()3.{4.m_paint = new Paint(Paint.ANTI_ALIAS_FLAG);5.LinearGradient lg = new LinearGradient(6.0, 0, 0, m_nShadowH,7.0xB0FFFFFF, 0x00000000,8.Shader.TileMode.CLAMP);9.m_paint.setShader(lg);10. m_paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));11.}12.13.@Override protected void14.onDraw(Canvas canvas)15.{16. super.onDraw(canvas);17.18. int nX = 0;19. int nY = 20;20.21. _DrawNormalImg(canvas, nX, nY);22. _DrawMirror(canvas, nX, nY);23.}24.25.private void26._DrawNormalImg(Canvas canvas, int nX, int nY)27.{28. canvas.save(Canvas.MATRIX_SAVE_FLAG);29. canvas.translate(nX, nY);30. m_dw.draw(canvas);31. canvas.restore();32.}33.34.private void35._DrawMirror(Canvas canvas, int nX, int nY)36.{37. int nW = m_dw.getIntrinsicWidth();38. int nH = m_dw.getIntrinsicHeight();39.40. ///////////////////////////////////41. //draw mirror image42. canvas.save(Canvas.MATRIX_SAVE_FLAG);43. canvas.scale(1.0f, -1.0f);44. canvas.translate(nX, -(nY + nH * 2));45. canvas.clipRect(0, nH, nW, nH - m_nShadowH);46. m_dw.draw(canvas);47. canvas.restore();48.49. //////////////////////////////50. //draw mask51. canvas.save();52. canvas.translate(nX, nY + nH);53. canvas.drawRect(0, 0, nW, m_nShadowH, m_paint);54. canvas.restore();55.}。