- 浏览: 116074 次
- 性别:
- 来自: 广州
文章分类
最新评论
Android中图片的处理(放大缩小,去色,转换格式,增加水印等),多张图片四个方位的图片合成,改变bitmap大小,图片去色等功能
Java代码 1.package com.dzh.operateimage;
2.import android.graphics.Bitmap;
3.import android.graphics.Bitmap.Config;
4.import android.graphics.BitmapFactory;
5.import android.graphics.Canvas;
6.import android.graphics.ColorMatrix;
7.import android.graphics.ColorMatrixColorFilter;
8.import android.graphics.Paint;
9.import android.graphics.PorterDuff.Mode;
10.import android.graphics.PorterDuffXfermode;
11.import android.graphics.Rect;
12.import android.graphics.RectF;
13.import android.graphics.drawable.BitmapDrawable;
14.import android.graphics.drawable.Drawable;
15.import java.io.ByteArrayOutputStream;
16.import java.io.File;
17.import java.io.FileNotFoundException;
18.import java.io.FileOutputStream;
19.import java.io.IOException;
20./**
21.* 处理图片的工具类.
22.*/
23.public class ImageTools {
24.public static final int LEFT = 0;
25.public static final int RIGHT = 1;
26.public static final int TOP = 3;
27.public static final int BOTTOM = 4;
28./** */
29./**
30.* 图片去色,返回灰度图片
31.*
32.* @param bmpOriginal 传入的图片
33.* @return 去色后的图片
34.*/
35.public static Bitmap toGrayscale(Bitmap bmpOriginal) {
36.int width, height;
37.height = bmpOriginal.getHeight();
38.width = bmpOriginal.getWidth();
39.Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
40.Canvas c = new Canvas(bmpGrayscale);
41.Paint paint = new Paint();
42.ColorMatrix cm = new ColorMatrix();
43.cm.setSaturation(0);
44.ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
45.paint.setColorFilter(f);
46.c.drawBitmap(bmpOriginal, 0, 0, paint);
47.return bmpGrayscale;
48.}
49./** */
50./**
51.* 去色同时加圆角
52.*
53.* @param bmpOriginal 原图
54.* @param pixels 圆角弧度
55.* @return 修改后的图片
56.*/
57.public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) {
58.return toRoundCorner(toGrayscale(bmpOriginal), pixels);
59.}
60./** */
61./**
62.* 把图片变成圆角
63.*
64.* @param bitmap 需要修改的图片
65.* @param pixels 圆角的弧度
66.* @return 圆角图片
67.*/
68.public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
69.Bitmap output = Bitmap
70..createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
71.Canvas canvas = new Canvas(output);
72.final int color = 0xff424242;
73.final Paint paint = new Paint();
74.final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
75.final RectF rectF = new RectF(rect);
76.final float roundPx = pixels;
77.paint.setAntiAlias(true);
78.canvas.drawARGB(0, 0, 0, 0);
79.paint.setColor(color);
80.canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
81.paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
82.canvas.drawBitmap(bitmap, rect, rect, paint);
83.return output;
84.}
85./** */
86./**
87.* 使圆角功能支持BitampDrawable
88.*
89.* @param bitmapDrawable
90.* @param pixels
91.* @return
92.*/
93.public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable, int pixels) {
94.Bitmap bitmap = bitmapDrawable.getBitmap();
95.bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));
96.return bitmapDrawable;
97.}
98./**
99.* 读取路径中的图片,然后将其转化为缩放后的bitmap
100.*
101.* @param path
102.*/
103.public static void saveBefore(String path) {
104.BitmapFactory.Options options = new BitmapFactory.Options();
105.options.inJustDecodeBounds = true;
106.// 获取这个图片的宽和高
107.Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回bm为空
108.options.inJustDecodeBounds = false;
109.// 计算缩放比
110.int be = (int)(options.outHeight / (float)200);
111.if (be <= 0)
112.be = 1;
113.options.inSampleSize = 2; // 图片长宽各缩小二分之一
114.// 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦
115.bitmap = BitmapFactory.decodeFile(path, options);
116.int w = bitmap.getWidth();
117.int h = bitmap.getHeight();
118.System.out.println(w + " " + h);
119.// savePNG_After(bitmap,path);
120.saveJPGE_After(bitmap, path);
121.}
122./**
123.* 保存图片为PNG
124.*
125.* @param bitmap
126.* @param name
127.*/
128.public static void savePNG_After(Bitmap bitmap, String name) {
129.File file = new File(name);
130.try {
131.FileOutputStream out = new FileOutputStream(file);
132.if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
133.out.flush();
134.out.close();
135.}
136.} catch (FileNotFoundException e) {
137.e.printStackTrace();
138.} catch (IOException e) {
139.e.printStackTrace();
140.}
141.}
142./**
143.* 保存图片为JPEG
144.*
145.* @param bitmap
146.* @param path
147.*/
148.public static void saveJPGE_After(Bitmap bitmap, String path) {
149.File file = new File(path);
150.try {
151.FileOutputStream out = new FileOutputStream(file);
152.if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
153.out.flush();
154.out.close();
155.}
156.} catch (FileNotFoundException e) {
157.e.printStackTrace();
158.} catch (IOException e) {
159.e.printStackTrace();
160.}
161.}
162./**
163.* 水印
164.*
165.* @param bitmap
166.* @return
167.*/
168.public static Bitmap createBitmapForWatermark(Bitmap src, Bitmap watermark) {
169.if (src == null) {
170.return null;
171.}
172.int w = src.getWidth();
173.int h = src.getHeight();
174.int ww = watermark.getWidth();
175.int wh = watermark.getHeight();
176.// create the new blank bitmap
177.Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
178.Canvas cv = new Canvas(newb);
179.// draw src into
180.cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src
181.// draw watermark into
182.cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印
183.// save all clip
184.cv.save(Canvas.ALL_SAVE_FLAG);// 保存
185.// store
186.cv.restore();// 存储
187.return newb;
188.}
189./**
190.* 图片合成
191.*
192.* @return
193.*/
194.public static Bitmap potoMix(int direction, Bitmap... bitmaps) {
195.if (bitmaps.length <= 0) {
196.return null;
197.}
198.if (bitmaps.length == 1) {
199.return bitmaps[0];
200.}
201.Bitmap newBitmap = bitmaps[0];
202.// newBitmap = createBitmapForFotoMix(bitmaps[0],bitmaps[1],direction);
203.for (int i = 1; i < bitmaps.length; i++) {
204.newBitmap = createBitmapForFotoMix(newBitmap, bitmaps[i], direction);
205.}
206.return newBitmap;
207.}
208.
209.private static Bitmap createBitmapForFotoMix(Bitmap first, Bitmap second, int direction) {
210.if (first == null) {
211.return null;
212.}
213.if (second == null) {
214.return first;
215.}
216.int fw = first.getWidth();
217.int fh = first.getHeight();
218.int sw = second.getWidth();
219.int sh = second.getHeight();
220.Bitmap newBitmap = null;
221.if (direction == LEFT) {
222.newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Config.ARGB_8888);
223.Canvas canvas = new Canvas(newBitmap);
224.canvas.drawBitmap(first, sw, 0, null);
225.canvas.drawBitmap(second, 0, 0, null);
226.} else if (direction == RIGHT) {
227.newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Config.ARGB_8888);
228.Canvas canvas = new Canvas(newBitmap);
229.canvas.drawBitmap(first, 0, 0, null);
230.canvas.drawBitmap(second, fw, 0, null);
231.} else if (direction == TOP) {
232.newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Config.ARGB_8888);
233.Canvas canvas = new Canvas(newBitmap);
234.canvas.drawBitmap(first, 0, sh, null);
235.canvas.drawBitmap(second, 0, 0, null);
236.} else if (direction == BOTTOM) {
237.newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Config.ARGB_8888);
238.Canvas canvas = new Canvas(newBitmap);
239.canvas.drawBitmap(first, 0, 0, null);
240.canvas.drawBitmap(second, 0, fh, null);
241.}
242.return newBitmap;
243.}
244./**
245.* 将Bitmap转换成指定大小
246.* @param bitmap
247.* @param width
248.* @param height
249.* @return
250.*/
251.public static Bitmap createBitmapBySize(Bitmap bitmap,int width,int height)
252.{
253.return Bitmap.createScaledBitmap(bitmap, width, height, true);
254.}
255./**
256.* Drawable 转 Bitmap
257.*
258.* @param drawable
259.* @return
260.*/
261.public static Bitmap drawableToBitmapByBD(Drawable drawable) {
262.BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
263.return bitmapDrawable.getBitmap();
264.}
265./**
266.* Bitmap 转 Drawable
267.*
268.* @param bitmap
269.* @return
270.*/
271.public static Drawable bitmapToDrawableByBD(Bitmap bitmap) {
272.Drawable drawable = new BitmapDrawable(bitmap);
273.return drawable;
274.}
275./**
276.* byte[] 转 bitmap
277.*
278.* @param b
279.* @return
280.*/
281.public static Bitmap bytesToBimap(byte[] b) {
282.if (b.length != 0) {
283.return BitmapFactory.decodeByteArray(b, 0, b.length);
284.} else {
285.return null;
286.}
287.}
288./**
289.* bitmap 转 byte[]
290.*
291.* @param bm
292.* @return
293.*/
294.public static byte[] bitmapToBytes(Bitmap bm) {
295.ByteArrayOutputStream baos = new ByteArrayOutputStream();
296.bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
297.return baos.toByteArray();
298.}
299.}
发表评论
文章已被作者锁定,不允许评论。
-
stopSelf()与stopSelf(startId)的区别
2016-04-14 14:44 1052startId:表示启动服务的次数 stopSelf(sta ... -
安装/卸载/替换成功广播接收
2016-01-08 11:59 742<receiver android:name=" ... -
ContentProivder
2015-12-09 09:26 453ContentProvider 我们实现了其中的几个方法 ... -
Android获取网络时间
2015-11-19 11:27 11001、实时获取: LocationManager locatio ... -
android animation知识
2015-11-14 11:47 535<?xml version="1.0" ... -
代码中设置android:drawableTop等
2015-11-14 10:42 971btnAttachment.setCompoundDrawab ... -
自定义View
2015-11-04 15:09 367一般来说,自定义View都会去重写onMeasure()方法, ... -
SimpleOnGestureListener
2015-11-04 12:03 483onScroll()缓慢滑动,手指滑动屏幕的过程中执行; on ... -
设置dialog在底部并宽度全屏显示
2015-11-02 11:53 2237pickerDialog.setContentView(vie ... -
横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效
2015-10-29 10:51 1711在之前的版本中都是在Manifest文件中设置Activity ... -
Dialog注意点
2015-10-28 17:14 469dialog:setCancelable与setCancele ... -
清除setBackgroundResource的原有值
2015-10-23 16:50 1098setBackgroundResource(0);清除原有的s ... -
接收隐式意图
2015-10-22 11:29 507想接收隐式意图,必须在他们的意图过滤器中配有:android. ... -
严苛模式
2015-10-14 20:25 1347//启用严苛模式,StrictMode可以用于捕捉发生在应用程 ... -
Android使用Animation完成动画保留最后一帧的办法
2015-09-08 10:43 2273在动画效果的xml中,在set标签中加上一条android:f ... -
控件getHeight和getWidth等于0的解决办法
2015-09-07 17:27 1333//监听layoutContainer是否初始化完成 l ... -
Service和Thread的区别?
2015-08-19 13:25 459servie是系统的组件,它由系统进程托管(servicema ... -
避免内存泄露
2015-08-18 19:21 4031、尽量避免在Activity使用static。 2、能使用A ... -
需要context的时候用activity还是application?
2015-08-17 17:02 668需要context的时候用activity还是applicat ... -
谈谈UI中, Padding和Margin有什么区别?
2015-08-10 17:18 1063Padding 文字对边框, margin是控件与控件之间的距 ...
相关推荐
Android中图片处理(放大缩小,去色,转换格式,增加水印等
主要功能:图片压缩,缩小图片尺寸,图片旋转,图片加文字或图片水印,图片格式转换,调整亮度与对比度,图片去色,去除图片Exif信息,CMYK转RGB等全部支持批量处理。 支持处理的图片格式:JPG, PNG, BMP, GIF(静态...
主要功能:图片压缩,缩小图片尺寸,图片旋转,图片加文字或图片水印,图片加边框,调整亮度、对比度和清晰度,图片去色,去除图片Exif信息(拍摄时间,相机型号信息),CMYK转RGB等,全部支持批量处理。 支持处理的...
图片压缩,缩小图片尺寸,图片旋转,图片加文字或图片水印,调整亮度与对比度,图片去色,去除图片Exif信息(拍摄时间,相机型号信息),CMYK转RGB等,全部支持批量处理。支持处理的图片格式:JPG, PNG, BMP, GIF(静态...
七彩色图片批量处理工具可以实现图片压缩,缩小图片尺寸,图片旋转,图片加文字或图片水印,调整亮度与对比度,图片去色,去除图片Exif信息(拍摄时间,相机型号信息),CMYK转RGB等,全部支持批量处理。支持处理的...
处理图片,图片去色,返回灰度图片,把图片变成圆角,图片合成
Js原生图片去色代码,将一张彩色图片变成黑白图片,由单纯的JavaScript图片处理特效,实现图片去色,让彩色的图片变成黑白的图片,过滤到了彩色,只留下黑白色阶。使用时,鼠标悬停于图片,点击图片上方的黑白转换...
图片去色,灰度处理,可以对图片进行灰度处理
主要介绍了android图片圆角、图片去色处理示例,需要的朋友可以参考下
图片去色工具,很好用,彩色转黑白小软件,可以进行图片的批量转色
VB图片去色 彩色变黑白图像,对图像进行去色处理,使彩色图片变为一张纯黑白的图片。
图片透明和去色计算及加深.rar
功能介绍 图片压缩,缩小图片尺寸,图片旋转,图片加文字或图片水印,调整亮度与对比度,图片去色,去除图片Exif信息,CMYK转RGB等,全部支持批量处理。 支持处理的图片格式:JPG, PNG, BMP, 静态GIF, TIF, PSD, ...
彩色图像转换为黑白图像时需要计算图像中每像素有效的亮度值,通过匹配像素 亮度值可以轻松转换为黑白图像。 计算像素有效的亮度值可以使用下面的公式: Y=0.3RED+0.59GREEN+0.11Blue 然后使用 Color.FromArgb(Y...
android端对图片的一些处理功能: 图片去色,返回灰度图片 去色同时加圆角 把图片变成圆角 使圆角功能支持BitampDrawable 读取路径中的图片,然后将其转化为缩放后的bitmap 保存图片为PNG 保存图片为JPEG 图片合成
图片去色 彩色变黑白图像,对图像进行去色处理,使彩色图片变为一张纯黑白的图片。
有两种方法把彩色图片进行去色,变成灰色图片。
Delphi图片去色,彩色变黑白,可把一张JPG图片变成黑白色,同时也可反相,生成底片效果。挺基础的一个Delphi图片处理例子。
图片透明和去色计算及加深减淡效果图片透明和去色计算及加深减淡效果的实现。学习图片计算的好例程。