我正在尝试在Android上的此入门标签检测MLKit上进行文本语音转换,但是在连接标签上的tts时遇到问题。我认为我的方法无效。该计划是“标签图形”将创建一个来自ImageLabelingProcessor的文本,但是我在将“标签图形”连接到我的“主活动”时遇到问题。任何帮助将非常感谢。
标签活动
class LabelActivity : AppCompatActivity(), OnRequestPermissionsResultCallback,
CompoundButton.OnCheckedChangeListener, TextToSpeech.OnInitListener {
private var cameraSource: CameraSource? = null
private var selectedModel = IMAGE_LABEL_DETECTION
var labelGraph = LabelGraphic(null, null)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_live_preview)
labelGraph.tts = TextToSpeech(this, this)
if (firePreview == null) {
Log.d(TAG, "Preview is null")
}
if (fireFaceOverlay == null) {
Log.d(TAG, "graphicOverlay is null")
}
if (allPermissionsGranted()) {
createCameraSource(selectedModel)
} else {
getRuntimePermissions()
}
}
private val requiredPermissions: Array<String?>
get() {
return try {
val info = this.packageManager
.getPackageInfo(this.packageName, PackageManager.GET_PERMISSIONS)
val ps = info.requestedPermissions
if (ps != null && ps.isNotEmpty()) {
ps
} else {
arrayOfNulls(0)
}
} catch (e: Exception) {
arrayOfNulls(0)
}
}
override fun onInit(status: Int) {
if (status == TextToSpeech.SUCCESS) {
// set US English as language for tts
val result = labelGraph.tts!!.setLanguage(Locale.US)
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS","The Language specified is not supported!")
}
} else {
Log.e("TTS", "Initilization Failed!")
}
}
override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
Log.d(TAG, "Set facing")
cameraSource?.let {
if (isChecked) {
it.setFacing(CameraSource.CAMERA_FACING_FRONT)
} else {
it.setFacing(CameraSource.CAMERA_FACING_BACK)
}
}
firePreview?.stop()
startCameraSource()
}
private fun createCameraSource(model: String) {
// If there's no existing cameraSource, create one.
if (cameraSource == null) {
cameraSource = CameraSource(this, fireFaceOverlay)
}
try {
Log.i(TAG, "Using Image Label Detector Processor")
cameraSource?.setMachineLearningFrameProcessor(ImageLabelingProcessor())
} catch (e: FirebaseMLException) {
Log.e(TAG, "can not create camera source: $model")
}
}
/**
* Starts or restarts the camera source, if it exists. If the camera source doesn't exist yet
* (e.g., because onResume was called before the camera source was created), this will be called
* again when the camera source is created.
*/
private fun startCameraSource() {
cameraSource?.let {
try {
if (firePreview == null) {
Log.d(TAG, "resume: Preview is null")
}
if (fireFaceOverlay == null) {
Log.d(TAG, "resume: graphOverlay is null")
}
firePreview?.start(cameraSource, fireFaceOverlay)
} catch (e: IOException) {
Log.e(TAG, "Unable to start camera source.", e)
cameraSource?.release()
cameraSource = null
}
}
}
public override fun onResume() {
super.onResume()
Log.d(TAG, "onResume")
startCameraSource()
}
/** Stops the camera. */
override fun onPause() {
super.onPause()
firePreview?.stop()
}
public override fun onDestroy() {
super.onDestroy()
cameraSource?.release()
// Shutdown TTS
if (labelGraph.tts != null) {
labelGraph.tts!!.stop()
labelGraph.tts!!.shutdown()
}
}
private fun allPermissionsGranted(): Boolean {
for (permission in requiredPermissions) {
if (!isPermissionGranted(this, permission!!)) {
return false
}
}
return true
}
private fun getRuntimePermissions() {
val allNeededPermissions = arrayListOf<String>()
for (permission in requiredPermissions) {
if (!isPermissionGranted(this, permission!!)) {
allNeededPermissions.add(permission)
}
}
if (!allNeededPermissions.isEmpty()) {
ActivityCompat.requestPermissions(
this, allNeededPermissions.toTypedArray(), PERMISSION_REQUESTS)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
Log.i(TAG, "Permission granted!")
if (allPermissionsGranted()) {
createCameraSource(selectedModel)
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
companion object {
private const val IMAGE_LABEL_DETECTION = "Label Detection"
private const val TAG = "LabelActivity"
private const val PERMISSION_REQUESTS = 1
private fun isPermissionGranted(context: Context, permission: String): Boolean {
if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Permission granted: $permission")
return true
}
Log.i(TAG, "Permission NOT granted: $permission")
return false
}
}
}
标签图形
class LabelGraphic(
private val overlay: GraphicOverlay?,
private val labels: List<FirebaseVisionImageLabel>?
) : GraphicOverlay.Graphic(overlay) {
var tts: TextToSpeech? = null
private val textPaint = Paint().apply {
color = Color.WHITE
textSize = 60.0f
}
@Synchronized
override fun draw(canvas: Canvas) {
val x = overlay!!.width / 4.0f
var y = overlay.height / 2.0f
if (labels != null) {
for (label in labels) {
canvas.drawText(label.text, x, y, textPaint)
y -= 62.0f
}
}
val firstLabel = labels!!.first().text
Log.d("LABEL GRAPHIC", firstLabel)
tts!!.speak(firstLabel, TextToSpeech.QUEUE_FLUSH, null,"")
}
}
现在,当我尝试运行此命令时,它会说LabelGraphic上的tts为空。