Android 电子词典拍照翻译功能设计思路

本功能旨在实现用户通过拍照的方式识别图片中的文字并将其保存到单词夹中,方便用户学习新的词汇。

设计思路

  1. 调用系统相机拍照,并将照片保存到本地。
  2. 使用OCR技术对照片中的文字进行识别。
  3. 将识别出的文字展示在界面上,并提供保存到单词夹的选项。
  4. 用户选择保存到单词夹后,将识别出的单词添加到指定的单词夹中。

详细设计

  1. 布局设计

    • 在布局文件中添加一个ImageView控件,用于显示拍摄的照片。
    • 添加一个TextView控件,用于展示识别出的文字。
    • 添加一个按钮,用于触发拍照功能。
    • 添加一个按钮,用于触发翻译功能。
    • 添加一个按钮,用于保存识别结果到单词夹。
  2. 拍照功能实现

    • 在Activity中添加一个变量,用于保存拍摄的照片:

      private Bitmap photoBitmap;
      
    • 在拍照按钮的点击事件中,调用系统相机拍照并将照片保存到本地:

      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      File photoFile = createImageFile();
      Uri photoUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", photoFile);
      intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
      startActivityForResult(intent, REQUEST_CODE_TAKE_PHOTO);
      
      • createImageFile()方法中,创建一个临时文件用于保存照片,并返回该文件的路径:

        private File createImageFile() throws IOException {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
            String imageFileName = "JPEG_" + timeStamp + "_";
            File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            File imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
            photoPath = imageFile.getAbsolutePath();
            return imageFile;
        }
        
  3. 照片展示

    • onActivityResult()方法中,获取拍摄的照片并显示在ImageView控件上:

      if (requestCode == REQUEST_CODE_TAKE_PHOTO && resultCode == RESULT_OK) {
          photoBitmap = BitmapFactory.decodeFile(photoPath);
          imgInto.setImageBitmap(photoBitmap);
      }
      
  4. 文字识别

    • 在翻译按钮的点击事件中,使用OCR技术对照片中的文字进行识别,并将识别出的文字展示在TextView控件上:

      TessBaseAPI tessBaseAPI = new TessBaseAPI();
      tessBaseAPI.init(getExternalFilesDir(null).getAbsolutePath(), "eng");
      tessBaseAPI.setImage(photoBitmap);
      String result = tessBaseAPI.getUTF8Text();
      editText.setText(result);
      
  5. 保存到单词夹

    • 在保存到单词夹的按钮点击事件中,弹出一个对话框让用户选择单词夹:

      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setSingleChoiceItems(choices, -1, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              // 处理保存到单词夹的逻辑
              dialog.dismiss();
          }
      }).show();
      
    • 在处理保存到单词夹的逻辑中,将识别出的单词添加到指定的单词夹中:

      String[] result = editText.getText().toString().toLowerCase().split("\n");
      if (result.length >= 1) {
          HashMap<Integer, Integer> map = new HashMap<>();
          for (int i = 0; i < result.length; ++i) {
              List<Word> words = LitePal.where("word = ?", result[i]).select("wordId", "word").find(Word.class);
              if (!words.isEmpty()) {
                  if (!map.containsValue(words.get(0).getWordId())) {
                      map.put(i, words.get(0).getWordId());
                  }
              }
          }
          if (which == 0) {
              // 选择已有单词夹
              final List<WordFolder> wordFolders = LitePal.findAll(WordFolder.class);
              if (!wordFolders.isEmpty()) {
                  String[] folderNames = new String[wordFolders.size()];
                  for (int i = 0; i < wordFolders.size(); ++i) {
                      folderNames[i] = wordFolders.get(i).getName();
                  }
                  AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
                  builder2.setSingleChoiceItems(folderNames, -1, new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          for (int ii : map.keySet()) {
                              List<FolderLinkWord> folderLinkWords = LitePal.where("wordId = ? and folderId = ?", map.get(ii) + "", wordFolders.get(which).getId() + "").find(FolderLinkWord.class);
                              if (folderLinkWords.isEmpty()) {
                                  FolderLinkWord folderLinkWord = new FolderLinkWord();
                                  folderLinkWord.setFolderId(wordFolders.get(which).getId());
                                  folderLinkWord.setWordId(map.get(ii));
                                  folderLinkWord.save();
                              }
                          }
                          Toast.makeText(OCRActivity.this, "保存成功!", Toast.LENGTH_SHORT).show();
                      }
                  }).show();
              } else {
                  Toast.makeText(OCRActivity.this, "当前暂无单词夹", Toast.LENGTH_SHORT).show();
              }
          } else {
              // 自动存入新建单词夹
              long currentTime = TimeController.getCurrentTimeStamp();
              WordFolder wordFolder = new WordFolder();
              wordFolder.setName("拍照翻译");
              wordFolder.setRemark("自动创建于:" + TimeController.getStringDateDetail(currentTime));
              wordFolder.setCreateTime(currentTime + "");
              if (wordFolder.save()) {
                  List<WordFolder> wordFolders = LitePal.where("createTime = ? and name = ?", currentTime + "", "拍照翻译").find(WordFolder.class);
                  if (!wordFolders.isEmpty()) {
                      for (int ii : map.keySet()) {
                          List<FolderLinkWord> folderLinkWords = LitePal.where("wordId = ? and folderId = ?", map.get(ii) + "", wordFolders.get(0).getId() + "").find(FolderLinkWord.class);
                          if (folderLinkWords.isEmpty()) {
                              FolderLinkWord folderLinkWord = new FolderLinkWord();
                              folderLinkWord.setFolderId(wordFolders.get(0).getId());
                              folderLinkWord.setWordId(map.get(ii));
                              folderLinkWord.save();
                          }
                      }
                  }
                  Toast.makeText(OCRActivity.this, "保存成功!", Toast.LENGTH_SHORT).show();
              }
          }
      }
      

流程图

  1. 用户点击拍照按钮,调用系统相机拍照并将照片保存到本地。
  2. 用户点击翻译按钮,使用OCR技术对照片中的文字进行识别,并将识别出的文字展示在界面上。
  3. 用户选择保存到单词夹的选项,弹出一个对话框让用户选择单词夹。
  4. 用户选择已有单词夹,弹出一个对话框让用户选择要保存到的单词夹。
  5. 用户选择要保存到的单词夹,将识别出的单词添加到指定的单词夹中。
  6. 用户选择自动存入新建单词夹,自动创建一个新的单词夹,并将识别出的单词添加到该单词夹中。

总结

通过以上设计思路和代码示例,可以实现一个简单的拍照翻译功能,帮助用户更方便地学习新的词汇。当然,实际开发中还需要考虑更多细节,例如错误处理、用户体验优化等。

Android 电子词典拍照翻译功能设计思路

原文地址: https://www.cveoy.top/t/topic/jT8y 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录