一、环境的搭建
Android studio下载地址:点击这里下载。
sun.misc.base64decoder.jar:http://www.pc6.com/softview/SoftView_456772.html
sun.misc.base64decoder.jar下载完成后倒入libs
二、界面搭建
三、功能实现
首先实现图片浏览功能:
在打开相册的点击事件中实现打开相册功能
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(); }
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { openAlbum(); } else { Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show(); } break; default: } }
private void openAlbum() { Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); startActivityForResult(intent, CHOOSE_PHOTO); }
在onActivityResult中
if (resultCode == RESULT_OK) { if (Build.VERSION.SDK_INT >= 19) { handleImageOnKitKat(data); } else { handleImageBeforeKitKat(data); } } break;
private void handleImageOnKitKat(Intent data) { String imagePath = null; Uri uri = data.getData(); Log.d("TAG", "handleImageOnKitKat: uri is " + uri); if (DocumentsContract.isDocumentUri(this, uri)) { String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1]; 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())) { imagePath = getImagePath(uri, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { imagePath = uri.getPath(); } displayImage(imagePath); } private void handleImageBeforeKitKat(Intent data) { Uri uri = data.getData(); String imagePath = getImagePath(uri, null); displayImage(imagePath); }
private void displayImage(String imagePath) { if (imagePath != null) { Bitmap bitmap = BitmapFactory.decodeFile(imagePath); mIvShowPic.setImageBitmap(bitmap); File file = new File(imagePath); try { InputStream inputStream = new FileInputStream(file); getOrcData(inputStream); } catch (FileNotFoundException e) { e.printStackTrace(); } } else { Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show(); } }
private void getOrcData(InputStream inputStream) { if (inputStream==null) { return; }else { show_text.setText(""); } try { byte[] data = new byte[inputStream.available()]; inputStream.read(data); //关闭流 inputStream.close(); BASE64Encoder encoder = new BASE64Encoder(); String encode = encoder.encode(data); getPerSonData(encode); Log.e("encode", "1----------------------------" + encode); } catch (IOException e) { e.printStackTrace(); } }
private void getPerSonData(final String encode) { new Thread(new Runnable() { @Override public void run() { try { Log.e("test", "2----------------------------"); JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("image", encode); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("side", "face"); jsonObject1.put("configure", jsonObject2.toString()); Log.e("jsonObject1", "" + jsonObject1.toString()); URL url = new URL("http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json"); //这里就用第三方开源库okhttp,,httpURlConnction太磨叽了 Log.e("aa", "aaaaaaaaaaaa"); OkHttpClient client = new OkHttpClient(); RequestBody requestBody = FormBody.create(MediaType.parse("application/json;charset=utf-8"), jsonObject1.toString()); Request request = new Request.Builder() .url(url) .header("accept", "*/*") .header("connection", "Keep-Alive") .header("Content-Type", "text/html;charset=utf-8")//41056833fb5d44c8826d72b5de3c472a可以去阿里云注册 .header("Authorization", "APPCODE 41056833fb5d44c8826d72b5de3c472a") //可以去阿里云注册 .method("POST", requestBody) .build(); Response response = client.newCall(request).execute(); if (response != null) { String string = response.body().string(); Log.e("aa", "aaaaaaaaaaaa"); Log.e("aa", "aaaaaaaaaaaa" + string); showText(string); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } }).start(); }
之所以这样写参数是因为阿里云给这样的接口
最后是显示文字(返回的是json,就不解析了):
private void showText(final String string) { runOnUiThread(new Runnable() { @Override public void run() { show_text.setText(string); } }); }
打开相机写法也类似,就只将文件流转换成64字节码,传数据就完事了
项目地址github: https://github.com/android-xiao-jun/Android-ocr-.git