This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
PaintView.java
234 lines (194 loc) · 6.62 KB
/
PaintView.java
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
package ibm.tf.hangul;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
public class PaintView extends View {
// Length in pixels of each dimension for the bitmap displayed on the screen.
public static final int BITMAP_DIMENSION = 128;
// Length in pixels of each dimension for the bitmap to be fed into the model.
public static final int FEED_DIMENSION = 64;
private boolean setup, drawHere;
private Paint paint;
private Bitmap bitmap;
private Path path;
private Canvas canvas;
private Matrix transformMat = new Matrix();
private Matrix inverseTransformMat = new Matrix();
private PointF pointF = new PointF();
private ArrayList<PaintPath> PaintPathList = new ArrayList<>();
private View drawTextView;
public PaintView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
path = new Path();
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(6);
}
/**
* Remove all strokes and clear the canvas.
*/
public void reset() {
path.reset();
if (bitmap != null) {
bitmap.eraseColor(Color.BLACK);
}
}
public void setDrawText(View view) {
drawTextView = view;
drawHere = true;
}
/**
* This function will create the transform matrix for scaling up and centering the bitmap
* that will represent our character image inside the view. The inverse matrix is also created
* for mapping touch coordinates to coordinates within the bitmap.
*/
private void setupScaleMatrices() {
// View size.
float width = getWidth();
float height = getHeight();
float scaleW = width / BITMAP_DIMENSION;
float scaleH = height / BITMAP_DIMENSION;
float scale = scaleW;
if (scale > scaleH) {
scale = scaleH;
}
// Translation to center bitmap in view after it is scaled up.
float centerX = BITMAP_DIMENSION * scale / 2;
float centerY = BITMAP_DIMENSION * scale / 2;
float dx = width / 2 - centerX;
float dy = height / 2 - centerY;
transformMat.setScale(scale, scale);
transformMat.postTranslate(dx, dy);
transformMat.invert(inverseTransformMat);
setup = true;
}
/**
* This gets the coordinates in the bitmap based on the the coordinates of where the
* user touched.
*/
public void getBitmapCoords(float x, float y, PointF out) {
float points[] = new float[2];
points[0] = x;
points[1] = y;
inverseTransformMat.mapPoints(points);
out.x = points[0];
out.y = points[1];
}
public void onResume() {
createBitmap();
}
public void onPause() {
releaseBitmap();
}
private void createBitmap() {
if (bitmap != null) {
bitmap.recycle();
}
bitmap = Bitmap.createBitmap(BITMAP_DIMENSION, BITMAP_DIMENSION, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
reset();
}
private void releaseBitmap() {
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
canvas = null;
}
reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// If this is the first time the user has touched the drawable canvas, hide the text that
// tells the user where to draw.
if (drawHere) {
drawTextView.setVisibility(View.INVISIBLE);
drawHere = false;
}
PaintPath paintPath = new PaintPath();
canvas.drawPath(path, paint);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
getBitmapCoords(event.getX(), event.getY(), pointF);
path.moveTo(pointF.x, pointF.y);
path.lineTo(pointF.x, pointF.y);
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
getBitmapCoords(event.getX(), event.getY(), pointF);
path.lineTo(pointF.x, pointF.y);
paintPath.setPath(path);
paint.setColor(Color.WHITE);
paintPath.setPaint(paint);
PaintPathList.add(paintPath);
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas c) {
if (!setup) {
setupScaleMatrices();
}
if (bitmap == null) {
return;
}
c.drawBitmap(bitmap, transformMat, paint);
if (PaintPathList.size() > 0) {
canvas.drawPath(
PaintPathList.get(PaintPathList.size() - 1).getPath(),
PaintPathList.get(PaintPathList.size() - 1).getPaint());
}
}
/**
* This function will convert the bitmap pixels to usable input to our TensorFlow model.
*/
public float[] getPixelData() {
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, FEED_DIMENSION,
FEED_DIMENSION, false);
int width = FEED_DIMENSION;
int height = FEED_DIMENSION;
int[] pixels = new int[width * height];
resizedBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
float[] returnPixels = new float[pixels.length];
// Here we want to convert each pixel to a floating point number between 0.0 and 1.0 with
// 1.0 being white and 0.0 being black.
for (int i = 0; i < pixels.length; ++i) {
int pix = pixels[i];
int b = pix & 0xff;
returnPixels[i] = (float) (b/255.0);
}
return returnPixels;
}
/**
* This is an object to encapsulate the path and paint information related to drawing on the
* bitmap/canvas.
*/
public class PaintPath {
Path path;
Paint paint;
public Path getPath() {
return path;
}
public void setPath(Path path) {
this.path = path;
}
public Paint getPaint() {
return paint;
}
public void setPaint(Paint paint) {
this.paint = paint;
}
}
}