手写数字识别(Android版)Android端

介绍

Android端实现的需求:

  • 调用手机相机拍摄手写好的数字图片
  • 从手机相册中选取手写好的数字图片
  • 将图片上传到服务器
  • 接收服务器返回的结果并显示

这里Android和服务器通信用的是okhttp3协议

问题

okhttp3:

导入:可以参考文章

若遇到异常:

1
No Network Security Config specified, using platform default

解决方法:参考文章

手机存储:

若遇到异常:

1
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0

在 res/xml/ 下新建一个 file_paths.xml 文件,然后往里写入:

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="." />
</paths>

然后在 AndroidManifest.xml 中添加:

1
2
3
4
5
6
7
8
9
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.identification.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

实现

就一个java文件,也是主活动,相册和相机都是常用的多媒体功能。

这里要注意的就是常见的子线程不能更新UI,UI要到主线程更新。

活动:

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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

public static final int TAKE_PHOTO = 1;
public static final int CHOOSE_PHOTO = 2;

/* 没连接上服务器 */
private static final int NON_INFORMATION = -1;

/* 返回结果 */
private static final int ZERO_INFORMATION = 0;
private static final int ONE_INFORMATION = 1;
private static final int TWO_INFORMATION = 2;
private static final int THREE_INFORMATION = 3;
private static final int FOUR_INFORMATION = 4;
private static final int FIVE_INFORMATION = 5;
private static final int SIX_INFORMATION = 6;
private static final int SEVEN_INFORMATION = 7;
private static final int EIGHT_INFORMATION = 8;
private static final int NINE_INFORMATION = 9;


/* 定义控件 */
private Button takePhoto;
private Button chooseFromAlbum;
private Button sendImageToServer;
private TextView myResult;
private ImageView picture;

private Uri imageUri;

private String myImagePath = "null";

private final MediaType MEDIA_TYPE_JPG = MediaType.parse("image/jpg");
private final OkHttpClient client = new OkHttpClient();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 创建控件对象
takePhoto = (Button) findViewById(R.id.take_photo);
chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);
sendImageToServer = (Button) findViewById(R.id.send_image);
myResult = (TextView) findViewById(R.id.get_result);
picture = (ImageView) findViewById(R.id.picture);

// 发送图片到服务器
sendImageToServer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!myImagePath.equals("null")) {
File f = new File(myImagePath);
try {
runUpload(f);
} catch (Exception e) {
e.printStackTrace();
}
}
else{
Toast.makeText(MainActivity.this, "没有输入图片的路径", Toast.LENGTH_SHORT).show();
}
}
});

// 调用摄像头的点击事件
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// 创建File对象,用于存储拍照后的图片
File outputImage = new File(getExternalCacheDir(), "output_image.jpg");

try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

// 判断系统版本并选择相应的方法,小于24表明版本低于7.0
if (Build.VERSION.SDK_INT < 24) {
imageUri = Uri.fromFile(outputImage);
} else {
imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.coalgangueidentification.fileprovider", outputImage);
}

// 启动相机程序
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

// 开启当前活动
startActivityForResult(intent, TAKE_PHOTO);

}
});

// 调用手机相册的点击事件
chooseFromAlbum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission. WRITE_EXTERNAL_STORAGE }, 1);
} else {
openAlbum();
}
}
});
}

/* 定义UI线程 */
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case NON_INFORMATION:
myResult.setText("暂时没有识别结果!");
break;
case ZERO_INFORMATION:
myResult.setText("识别结果为:0");
break;
case ONE_INFORMATION:
myResult.setText("识别结果为:1");
break;
case TWO_INFORMATION:
myResult.setText("识别结果为:2");
break;
case THREE_INFORMATION:
myResult.setText("识别结果为:3");
break;
case FOUR_INFORMATION:
myResult.setText("识别结果为:4");
break;
case FIVE_INFORMATION:
myResult.setText("识别结果为:5");
break;
case SIX_INFORMATION:
myResult.setText("识别结果为:6");
break;
case SEVEN_INFORMATION:
myResult.setText("识别结果为:7");
break;
case EIGHT_INFORMATION:
myResult.setText("识别结果为:8");
break;
case NINE_INFORMATION:
myResult.setText("识别结果为:9");
break;
default:
break;
}
}
};

/* 打开手机相册 */
private void openAlbum() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");

// 打开相册
startActivityForResult(intent, CHOOSE_PHOTO);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openAlbum();
} else {
Toast.makeText(this, "你拒绝了打开相册请求!", Toast.LENGTH_SHORT).show();
}
break;
default:
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {

// 拍照获取图片
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
try {

// 将拍摄的照片显示出来
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);

// 将拍摄到的图片保存到相册当中
String saveToMyAlbum = saveImageToGallery(this, bitmap);

// 若照片保存成功,则把照片路径赋值到要传输照片的路径,否则提示
if (!saveToMyAlbum.equals("false")){
myImagePath = saveToMyAlbum;
}
else{
Toast.makeText(this, "图片没有保存成功", Toast.LENGTH_LONG).show();
}

} catch (Exception e) {
e.printStackTrace();
}
}
break;

// 从相册选择照片获取图片
case CHOOSE_PHOTO:
if (resultCode == RESULT_OK) {
// 判断手机系统版本号
if (Build.VERSION.SDK_INT >= 19) {
// 4.4及以上系统使用这个方法处理图片
handleImageOnKitKat(data);
} else {
// 4.4以下系统使用这个方法处理图片
handleImageBeforeKitKat(data);
}
}
break;
default:
break;
}
}

@TargetApi(19)
private void handleImageOnKitKat(Intent data) {
String imagePath = null;
Uri uri = data.getData();
Log.d("TAG", "handleImageOnKitKat: uri is " + uri);
if (DocumentsContract.isDocumentUri(this, uri)) {
// 如果是document类型的Uri,则通过document id处理
String docId = DocumentsContract.getDocumentId(uri);
if("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id = docId.split(":")[1]; // 解析出数字格式的id
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
imagePath = getImagePath(contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
// 如果是content类型的Uri,则使用普通方式处理
imagePath = getImagePath(uri, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
// 如果是file类型的Uri,直接获取图片路径即可
imagePath = uri.getPath();
}
displayImage(imagePath); // 根据图片路径显示图片
}

private void handleImageBeforeKitKat(Intent data) {
Uri uri = data.getData();
String imagePath = getImagePath(uri, null);
displayImage(imagePath);
}

@SuppressLint("Range")
private String getImagePath(Uri uri, String selection) {
String path = null;
// 通过Uri和selection来获取真实的图片路径
Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}

private void displayImage(String imagePath) {
if (imagePath != null) {
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
picture.setImageBitmap(bitmap);

// 赋值给要传送到服务器的图片路径
myImagePath = imagePath;
} else {
Toast.makeText(this, "图片获取失败!", Toast.LENGTH_SHORT).show();
}
}

/* 保存文件到指定路径 */
public String saveImageToGallery(Context context, Bitmap bmp) {

// 将图片保存
String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "dearxy";
File appDir = new File(storePath);

if (!appDir.exists()) {
appDir.mkdir();
}

String fileName = System.currentTimeMillis() + ".jpg";

File file = new File(appDir, fileName);
try {

FileOutputStream fos = new FileOutputStream(file);

// 通过io流的方式来压缩保存图片
boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);
fos.flush();
fos.close();

// 保存图片后发送广播通知更新数据库
Uri uri = Uri.fromFile(file);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));

// 如果保存成功则返回保存路径,否则返回错误码
if (isSuccess) {
return (storePath + "/" + fileName);
} else {
return "false";
}

} catch (IOException e) {
e.printStackTrace();
}
return "false";
}

/* 运行上传线程 */
public void runUpload(File f) throws Exception {
final File file = f;
new Thread(() -> {

// 子线程需要做的工作
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file","image.jpg",
RequestBody.create(MEDIA_TYPE_JPG, file))
.build();

// 设置为自己的ip地址
Request request = new Request.Builder()
.url(getString(R.string.server_ip))
.post(requestBody)
.build();

// 获取请求回应对象
Call call = client.newCall(request);

// 对回应进行的处理
call.enqueue(new Callback() {

@Override
// 当无法连接上服务器时,发出提示信号
public void onFailure(Call call, IOException e) {
runOnUiThread(() -> {
sendMessage(NON_INFORMATION);
});
}

@Override
// 当连接上服务器时,对请求的回复进行响应
public void onResponse(Call call, final Response response) throws IOException {

// 获取回调的字符串信息
final String res = response.body().string();

runOnUiThread(() -> {

switch(res.charAt(0)) {
case '0':
sendMessage(ZERO_INFORMATION);
break;
case '1':
sendMessage(ONE_INFORMATION);
break;
case '2':
sendMessage(TWO_INFORMATION);
break;
case '3':
sendMessage(THREE_INFORMATION);
break;
case '4':
sendMessage(FOUR_INFORMATION);
break;
case '5':
sendMessage(FIVE_INFORMATION);
break;
case '6':
sendMessage(SIX_INFORMATION);
break;
case '7':
sendMessage(SEVEN_INFORMATION);
break;
case '8':
sendMessage(EIGHT_INFORMATION);
break;
case '9':
sendMessage(NINE_INFORMATION);
break;
default:
break;
}
});
}
});
}).start();
}

// 发送给UI线程显示信息
public void sendMessage(int information) {

runOnUiThread(() -> {

Message msg = new Message();
msg.what = information;
handler.sendMessage(msg);

if (information == NON_INFORMATION) {
// 提示连接服务器失败信号
Toast.makeText(MainActivity.this, "服务器错误", Toast.LENGTH_SHORT).show();
}
});
}

}

界面布局:

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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.example.identification.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<ImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="550dp"
android:layout_gravity="center_horizontal" />

</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/get_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/red"
android:textSize="30sp"

android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/send_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:backgroundTint="@color/blue"
android:textSize="30sp"
android:text="开始识别"

android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/take_photo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/white"
android:backgroundTint="@color/blue"
android:textSize="30sp"
android:text="拍照"

android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>

<Button
android:id="@+id/choose_from_album"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/white"
android:backgroundTint="@color/blue"
android:textSize="30sp"
android:text="相册"

android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>

</LinearLayout>

</LinearLayout>
</LinearLayout>
</RelativeLayout>

布局效果:

AndroidManifest.xml文件

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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.identification">

<!--申请读写SD卡权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />

<!--申请网络权限-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:theme="@style/Theme.Identification"
android:requestLegacyExternalStorage="true">



<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.identification.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

</application>

</manifest>

res/values/内的文件:

colors.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="blue">#6699ff</color>
<color name="red">#EE0000</color>
</resources>

strings.xml

1
2
3
4
<resources>
<string name="app_name">数字识别</string>
<string name="server_ip">http://[服务器的IP]:[服务器的端口号]/upload</string>
</resources>

res/xml/内的文件:

这个文件夹和里面的文件都是自己新建的。

file_paths.xml

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="." />
</paths>

network_security_config.xml

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<network-security-config> //默认配置:允许明文通信
<base-config cleartextTrafficPermitted="true" />
</network-security-config>

效果