forked from daizhenjun/ImageFilterForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.java
More file actions
298 lines (248 loc) · 6.98 KB
/
Copy pathImage.java
File metadata and controls
298 lines (248 loc) · 6.98 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* HaoRan ImageFilter Classes v0.1
* Copyright (C) 2012 Zhenjun Dai
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation.
*/
package HaoRan.ImageFilter;
import java.nio.IntBuffer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.graphics.Matrix;
/**
*
* @author daizhj
*
*/
public class Image {
//original bitmap image
public Bitmap image;
public Bitmap destImage;
//format of image (jpg/png)
private String formatName;
//dimensions of image
private int width, height;
// RGB Array Color
protected int[] colorArray;
public Image(Bitmap img){
this.image = img;
formatName = "jpg";
width = img.getWidth();
height = img.getHeight();
destImage = Bitmap.createBitmap(width, height, Config.ARGB_8888);
updateColorArray();
}
public Image clone(){
return new Image(this.image);
}
/**
* Method to reset the image to a solid color
* @param color - color to rest the entire image to
*/
public void clearImage(int color){
for(int y=0; y<height; y++){
for(int x=0; x<width; x++){
setPixelColor(x, y, color);
}
}
}
/**
* Set color array for image - called on initialisation
* by constructor
*
* @param bitmap
*/
private void updateColorArray(){
colorArray = new int[width * height];
image.getPixels(colorArray, 0, width, 0, 0, width, height);
int r, g, b;
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
int index = y * width + x;
r = (colorArray[index] >> 16) & 0xff;
g = (colorArray[index] >> 8) & 0xff;
b = colorArray[index] & 0xff;
colorArray[index] = 0xff000000 | (b << 16) | (g << 8) | r;//android系统与window系统的rgb存储方式相反
}
}
}
/**
* Method to set the color of a specific pixel
*
* @param x
* @param y
* @param color
*/
public void setPixelColor(int x, int y, int color){
colorArray[((y*image.getWidth()+x))] = color;
//image.setPixel(x, y, color);
//destImage.setPixel(x, y, colorArray[((y*image.getWidth()+x))]);
}
/**
* Get the color for a specified pixel
*
* @param x
* @param y
* @return color
*/
public int getPixelColor(int x, int y){
return colorArray[y*width+x];
}
/**
* Set the color of a specified pixel from an RGB combo
*
* @param x
* @param y
* @param c0
* @param c1
* @param c2
*/
public void setPixelColor(int x, int y, int c0, int c1, int c2){
int rgbcolor = (255 << 24) + (c0 << 16) + (c1 << 8) + c2;
colorArray[((y*image.getWidth()+x))] = rgbcolor;
//int array = ((y*image.getWidth()+x));
//vbb.order(ByteOrder.nativeOrder());
//vertexBuffer = vbb.asFloatBuffer();
//vertexBuffer.put(vertices);
//vertexBuffer.position(0);
//image.setPixel(x, y, colorArray[((y*image.getWidth()+x))]);
}
public void copyPixelsFromBuffer() { //从缓冲区中copy数据以加快像素处理速度
IntBuffer vbb = IntBuffer.wrap(colorArray);
//vbb.put(colorArray);
destImage.copyPixelsFromBuffer(vbb);
vbb.clear();
//vbb = null;
}
/**
* Method to get the RED color for the specified
* pixel
* @param x
* @param y
* @return color of R
*/
public int getRComponent(int x, int y){
return (getColorArray()[((y*width+x))]& 0x00FF0000) >>> 16;
}
/**
* Method to get the GREEN color for the specified
* pixel
* @param x
* @param y
* @return color of G
*/
public int getGComponent(int x, int y){
return (getColorArray()[((y*width+x))]& 0x0000FF00) >>> 8;
}
/**
* Method to get the BLUE color for the specified
* pixel
* @param x
* @param y
* @return color of B
*/
public int getBComponent(int x, int y){
return (getColorArray()[((y*width+x))] & 0x000000FF);
}
/**
* Method to rotate an image by the specified number of degrees
*
* @param rotateDegrees
*/
public void rotate (int rotateDegrees){
Matrix mtx = new Matrix();
mtx.postRotate(rotateDegrees);
image = Bitmap.createBitmap(image, 0, 0, width, height, mtx, true);
width = image.getWidth();
height = image.getHeight();
updateColorArray();
}
/**
* @return the image
*/
public Bitmap getImage() {
//return image;
return destImage;
}
/**
* @param image the image to set
*/
public void setImage(Bitmap image) {
this.image = image;
}
/**
* @return the formatName
*/
public String getFormatName() {
return formatName;
}
/**
* @param formatName the formatName to set
*/
public void setFormatName(String formatName) {
this.formatName = formatName;
}
/**
* @return the width
*/
public int getWidth() {
return width;
}
/**
* @param width the width to set
*/
public void setWidth(int width) {
this.width = width;
}
/**
* @return the height
*/
public int getHeight() {
return height;
}
/**
* @param height the height to set
*/
public void setHeight(int height) {
this.height = height;
}
/**
* @return the colorArray
*/
public int[] getColorArray() {
return colorArray;
}
/**
* @param colorArray the colorArray to set
*/
public void setColorArray(int[] colorArray) {
this.colorArray = colorArray;
}
public static int SAFECOLOR(int a) {
if (a < 0)
return 0;
else if (a > 255)
return 255;
else
return a;
}
//;R.drawable.image
public static Image LoadImage(Activity activity, int resourceId)
{
Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), resourceId);
return new Image(bitmap);
}
}