-
Notifications
You must be signed in to change notification settings - Fork 0
/
Other.java
367 lines (317 loc) · 10.4 KB
/
Other.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package printer;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Environment;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class Other
{
public byte[] buf;
public int index;
private static final int WIDTH_80 = 576;
private static final int WIDTH_58 = 384;
private static int[] p0 = { 128 }; // P0 to P6 were octal?
private static int[] p1 = { 64 };
private static int[] p2 = { 32 };
private static int[] p3 = { 16 };
private static int[] p4 = { 8 };
private static int[] p5 = { 4 };
private static int[] p6 = { 2 };
public Other(int length)
{
buf = new byte[length];
index = 0;
}
public static StringBuilder RemoveChar(String str, char c)
{
StringBuilder sb = new StringBuilder();
int length = str.length();
for (int i = 0; i < length; i++)
{
char tmp = str.charAt(i);
if (tmp != c)
sb.append(tmp);
}
return sb;
}
public static boolean IsHexChar(char c)
{
return ((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) || (
(c >= 'A') && (c <= 'F'));
}
private static final byte[] chartobyte = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15 };
public static byte HexCharsToByte(char ch, char cl)
{
byte b = (byte) (chartobyte[(ch - '0')] << 4 & 0xF0 | chartobyte[(cl - '0')] & 0xF);
return b;
}
public static byte[] HexStringToBytes(String str)
{
int count = str.length();
byte[] data = null;
if (count % 2 == 0)
{
data = new byte[count / 2];
for (int i = 0; i < count; i += 2)
{
char ch = str.charAt(i);
char cl = str.charAt(i + 1);
if ((IsHexChar(ch)) && (IsHexChar(cl)))
{
if (ch >= 'a')
ch = (char) (ch - ' ');
if (cl >= 'a')
cl = (char) (cl - ' ');
data[(i / 2)] = HexCharsToByte(ch, cl);
}
else
{
data = null;
break;
}
}
}
return data;
}
public void UTF8ToGBK(String Data)
{
try
{
byte[] bs = Data.getBytes("GBK");
int DataLength = bs.length;
for (int i = 0; i < DataLength; i++)
{
buf[(index++)] = bs[i];
}
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
public static byte[] StringTOGBK(String data)
{
byte[] buffer = null;
try
{
buffer = data.getBytes("GBK");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return buffer;
}
public static Bitmap createAppIconText(Bitmap icon, String txt, float size, boolean is58mm, int hight)
{
if (is58mm)
{
Bitmap canvasBitmap = Bitmap.createBitmap(384, hight, Bitmap.Config.ARGB_8888);
int width = canvasBitmap.getWidth();
Canvas canvas = new Canvas(canvasBitmap);
canvas.setBitmap(canvasBitmap);
canvas.drawColor(-1);
TextPaint paint = new TextPaint();
paint.setColor(Color.rgb(0, 0, 0));
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setFakeBoldText(false);
StaticLayout layout = new StaticLayout(txt, 0, txt.length(), paint, width, Layout.Alignment.ALIGN_NORMAL, 1.1F, 0.0F, true, TextUtils.TruncateAt.END, width);
canvas.translate(0.0F, 5.0F);
layout.draw(canvas);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return canvasBitmap;
}
Bitmap canvasBitmap = Bitmap.createBitmap(576, hight, Bitmap.Config.ARGB_8888);
int width = canvasBitmap.getWidth();
Canvas canvas = new Canvas(canvasBitmap);
canvas.setBitmap(canvasBitmap);
canvas.drawColor(-1);
TextPaint paint = new TextPaint();
paint.setColor(Color.rgb(0, 0, 0));
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setFakeBoldText(false);
StaticLayout layout = new StaticLayout(txt, 0, txt.length(), paint, width, Layout.Alignment.ALIGN_NORMAL, 1.1F, 0.0F, true, TextUtils.TruncateAt.END, width);
canvas.translate(0.0F, 5.0F);
layout.draw(canvas);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return canvasBitmap;
}
public static byte[] byteArraysToBytes(byte[][] data)
{
int length = 0;
for (int i = 0; i < data.length; i++)
length += data[i].length;
byte[] send = new byte[length];
int k = 0;
for (int i = 0; i < data.length; i++)
for (int j = 0; j < data[i].length; j++)
send[(k++)] = data[i][j];
return send;
}
public static Bitmap resizeImage(Bitmap bitmap, int w, int h)
{
Bitmap BitmapOrg = bitmap;
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = w;
int newHeight = h;
float scaleWidth = newWidth / width;
float scaleHeight = newHeight / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true);
return resizedBitmap;
}
public static Bitmap toGrayscale(Bitmap bmpOriginal)
{
int height = bmpOriginal.getHeight();
int width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0.0F);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0.0F, 0.0F, paint);
return bmpGrayscale;
}
public static void saveMyBitmap(Bitmap mBitmap, String name)
{
File f = new File(Environment.getExternalStorageDirectory().getPath(),
name);
try
{
f.createNewFile();
}
catch (IOException localIOException)
{
}
FileOutputStream fOut = null;
try
{
fOut = new FileOutputStream(f);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
}
catch (FileNotFoundException localFileNotFoundException)
{
}
catch (IOException localIOException1)
{
}
}
public static byte[] thresholdToBWPic(Bitmap mBitmap)
{
int[] pixels = new int[mBitmap.getWidth() * mBitmap.getHeight()];
byte[] data = new byte[mBitmap.getWidth() * mBitmap.getHeight()];
mBitmap.getPixels(pixels, 0, mBitmap.getWidth(), 0, 0,
mBitmap.getWidth(), mBitmap.getHeight());
format_K_threshold(pixels, mBitmap.getWidth(),
mBitmap.getHeight(), data);
return data;
}
private static void format_K_threshold(int[] orgpixels, int xsize, int ysize, byte[] despixels)
{
int graytotal = 0;
int grayave = 128;
int k = 0;
for (int i = 0; i < ysize; i++)
{
for (int j = 0; j < xsize; j++)
{
int gray = orgpixels[k] & 0xFF;
graytotal += gray;
k++;
}
}
grayave = graytotal / ysize / xsize;
k = 0;
for (int i = 0; i < ysize; i++)
{
for (int j = 0; j < xsize; j++)
{
int gray = orgpixels[k] & 0xFF;
if (gray > grayave)
{
despixels[k] = 0;
}
else
{
despixels[k] = 1;
}
k++;
}
}
}
public static void overWriteBitmap(Bitmap mBitmap, byte[] dithered)
{
int ysize = mBitmap.getHeight();
int xsize = mBitmap.getWidth();
int k = 0;
for (int i = 0; i < ysize; i++)
{
for (int j = 0; j < xsize; j++)
{
if (dithered[k] == 0)
{
mBitmap.setPixel(j, i, -1);
}
else
{
mBitmap.setPixel(j, i, Color.rgb(0, 0, 0));
}
k++;
}
}
}
public static byte[] eachLinePixToCmd(byte[] src, int nWidth, int nMode)
{
int nHeight = src.length / nWidth;
int nBytesPerLine = nWidth / 8;
byte[] data = new byte[nHeight * (8 + nBytesPerLine)];
int offset = 0;
int k = 0;
for (int i = 0; i < nHeight; i++)
{
offset = i * (8 + nBytesPerLine);
data[(offset + 0)] = 29;
data[(offset + 1)] = 118;
data[(offset + 2)] = 48;
data[(offset + 3)] = ((byte) (nMode & 0x1));
data[(offset + 4)] = ((byte) (nBytesPerLine % 256));
data[(offset + 5)] = ((byte) (nBytesPerLine / 256));
data[(offset + 6)] = 1;
data[(offset + 7)] = 0;
for (int j = 0; j < nBytesPerLine; j++)
{
data[(offset + 8 + j)] =
((byte) (p0[src[k]] + p1[src[(k + 1)]] + p2[src[(k + 2)]] + p3[src[(k + 3)]] + p4[src[(k + 4)]] + p5[src[(k + 5)]] + p6[src[(k + 6)]] + src[(k + 7)]));
k += 8;
}
}
return data;
}
}