问题描述
截至目前,用户可以选择上传图片或使用相机拍摄,然后将其放入 ImageView。紧接着,用户可以使用按钮开始文本识别。
但是由于我在 onActivity 结果函数的 If 语句中创建了保存所选图片的变量,因此我不确定从何处启动所选图像的变量以在 startRecognition 函数中访问它。
class Scanner : Fragment() {
private lateinit var imageView: ImageView
private lateinit var photoFile: File
private lateinit var textView: TextView
private lateinit var btnAnalyze: Button
private val FILE_NAME= "photo.jpg"
private val REQUEST_CODE_KAMERA = 42
private val REQUEST_CODE_GALLERY = 69
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?):
View? {val view: View = inflater!!.inflate(R.layout.fragment_scan,container,false)
val btnTakePicture: Button = view.findViewById(R.id.btnTakePicture)
imageView = view.findViewById(R.id.imageView)
btnAnalyze = view.findViewById(R.id.btnAnalyze)
//Button Picture
btnTakePicture.setOnClickListener {
val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
builder.setTitle("Choose a Receipt")
builder.setMessage("Either upload a Receipt or take a picture of one")
// add the buttons
builder.setPositiveButton("Use camera",DialogInterface.OnClickListener { dialog,which ->
val takePictureIntent: Intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
photoFile = getPhotoFile(FILE_NAME)
val fileProvider = FileProvider.getUriForFile(requireActivity(),"com.example.nlp_expense_tracker.fileprovider",photoFile)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,fileProvider)
startActivityForResult(takePictureIntent,REQUEST_CODE_KAMERA)
})
builder.setNegativeButton("Upload from gallery",which ->
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent,REQUEST_CODE_GALLERY)
})
val dialog: AlertDialog = builder.create()
dialog.show()
//Button Analyze
btnAnalyze.setOnClickListener{
startTextRecognition()
}
}
return view
}
private fun getPhotoFile(fileName: String): File {
val storageDirectory = activity?.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(fileName,".jpg",storageDirectory)
}
override fun onActivityResult(requestCode: Int,resultCode: Int,data: Intent?) {
if (requestCode == REQUEST_CODE_KAMERA && resultCode == Activity.RESULT_OK) { //Wenn Kamera ausgewählt wird
val choosenImage =
BitmapFactory.decodeFile(photoFile.absolutePath) //<- Path des Fotos welches wir gemacht haben
imageView.setImageBitmap(choosenImage)
} else if (requestCode == REQUEST_CODE_GALLERY && resultCode == Activity.RESULT_OK) { //Wenn Galerie ausgewählt wird
val imageUri = data?.data
try {
val choosenImage =
MediaStore.Images.Media.getBitmap(activity?.getContentResolver(),imageUri)
imageView.setImageBitmap(choosenImage)
} catch (e: IOException) {
e.printStackTrace()
}
}
super.onActivityResult(requestCode,resultCode,data)
}
private fun startTextRecognition(){
textView = view!!.findViewById(R.id.btnAnalyze)
val inputImage = InputImage.fromBitmap(choosenImage,0)
val recognizer = TextRecognition.getClient()
recognizer.process(inputImage)
.addOnSuccessListener {
textView.text = it.text
Log.d(TAG,"sucessful")
}
.addOnFailureListener {
Log.d(TAG,"not sucessful",it)
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)