Android ML Kit Translator.getClient() Unresolved reference - android

I am following this explanation and having a problem with this code:
val options = TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.GERMAN) .build()
val englishGermanTranslator = Translator.getClient(options)
Only getClient() remained in red. Everything else was properly imported with Alt+Enter.
Am I missing something?

Seems to be an error/typo in the documentation. Translator should be changed to Translation:
val englishGermanTranslator = Translation.getClient(options)
You might want to create an issue.

Related

SonarLint: Suppress specific line warning in Android Studio

There is a special case in our code where we need to call setText with getText of the same TextView. SonarLint warn this with kotlin:S1656, to suppress it we use #SuppressWarnings annotation but it does not work. It started showing an error to the IDE and the warning from SonarLint still there. //NOSONAR is not working as well
No action is available as well for suppressing it.
Can you put the SuppressWarnings annotation before your method?
// Sonarlint plugin version 6.8.0.50884
#SuppressWarnings("kotlin:S1656")
fun yourMethod() {
// ...
withContext(coroutineDispatcherMain) {
// Set the text again to fix images overlapping text
htmlTextView.text = htmlTextView.text
}
}
I also wonder that if htmlTextView.invalidate() would work for you.
UPDATE:
SuppressWarnings annotation is also available for LOCAL_VARIABLE but I think the problem is more complicated in kotlin code.
If you examine the byte code of these lines below, you can see that some.num = ... is some.setNum(...) in byte code.
Sonar plugin's code analyser may not handle this kind of kotlin specific codes.
class Sonar {
private val some = Something(0)
private var member = 0
#Deprecated("...")
fun myNumber(): Int = 1
fun doThat() {
// local variable working
#SuppressWarnings("kotlin:S1874") // Code annotated as deprecated should not be used
val localNum = myNumber()
// not working
#SuppressWarnings("kotlin:S1874")
some.num = myNumber()
// not working
#SuppressWarnings("kotlin:S1874")
member = myNumber()
// ...
}
}
I found the docs for the issue to confirm its the right number: https://rules.sonarsource.com/kotlin/RSPEC-1656
Also read the source at https://github.com/SonarSource/sonarlint-intellij but not much help.
It seems like "// NOSONAR" is what you want (I know you said you tried it, but did you have the space? And have it at the end of the line?)
htmlTextView.text = htmlTextView.text // NOSONAR
Just a guess here, but you could also try:
#SuppressWarnings("RSPEC:S1656")
lastly to check suppress is working you can ignore everything:
#SuppressWarnings("all")
htmlTextView.text = htmlTextView.text
Big discussion here: Turning Sonar off for certain code
This article also gives you options to choose from: https://docs.codescan.io/hc/en-us/articles/360012109711-Ignoring-violations

HOW TO run pose estimation on single image with TensorFlow-Lite?

I recently used this sample of great TensorFlow lite in android.
I can use this project correctly, but I want to estimate poses on single images too (not just in real time mode). so I tried to reach my goal! but unfortunately I couldn't! and those disappointing codes are here:
private fun runOnSimpleImage() {
val detector = MoveNet.create(this, device, ModelType.Lightning)
detector.let { detector ->
simpleDetector = detector
}
simpleDetector?.estimatePoses(templateBitmap)?.let {persons->
VisualizationUtils.drawBodyKeypoints(
templateBitmap,
persons, false
)
}
showOutputBitmap(templateBitmap)
}
Also I search it and found this. but I couldn't solve my problem yet.
and my result is something like this:
Fortunately my code is not wrong! and it works correctly. and you can use it!
The problem was in method that I used to convert my drawable image to Bitmap.
I used to use these codes:
val drawable = ResourcesCompat.getDrawable(resources, R.drawable.resized,theme)
templateBitmap = (drawable as BitmapDrawable).bitmap
But when I changed those to something like this:
templateBitmap = BitmapFactory.decodeResource(resources,R.drawable.resized)
my problem solved.

Unresolved reference for Modifier.preferredSize and Modifier.prefferredHeight [closed]

I am following this YouTube Tutorial where they are using Modifier.preferredSize() on a box and Modifier.preferredHeight() on a Spacer Composable - all other chained Modifiers are fine.
However Android Studio is not recognizing these 2 options.
Here is the code that I am working with:
Column() {
var isBlue by remember { mutableStateOf(false) }
val color = if(isBlue) Color.Blue else Color.Green
Button(onClick = { isBlue = !isBlue }) {}
Spacer(modifier = Modifier.preferredHeight(128.dp))
Box(modifier = Modifier.preferredHeight(128.dp).background(color = color)){}
}
The Editor is high-lighting preferredHeight as unresolved.
This is the image from the IDE for perspective.
I am using compose_version = '1.0.1' and I'm on AS Arctic Fox
preferredSize was renamed to size and preferredHeight to height
If I face some old video/article with invalid api, I'm searching through compose-samples(official samples from the maintainers) commits to find place where this method was deprecated/renamed, it's the easiest way to know if it just was renamed or I need to change more logic. In this case change was on this commit
Modifier.preferredWidth/preferredHeight/preferredSize were renamed to width/height/size starting from 1.0.0-beta01
I resolved it by following the mouse over suggestion, pressing the alt+Enter to import automatically the required referenced packages
I am following the lessons for andoid and kotlin and had stacked with this error.

Android CameraX stuck with two use cases

I'm novice in Android development (more Python and ML engineer) but wanted to try this example from TensorFlow: TF Lite Transfer Learning.
I succesfully run it on Android Studio but spotted that I cannot do anything with the app as it works extraordinary slow. I was digging through the code to find a root cause a found out this.
In function CameraFragment::startCamera() a preview context is created
PreviewConfig config = new PreviewConfig.Builder()
.setLensFacing(LENS_FACING)
.setTargetAspectRatio(screenAspectRatio)
.setTargetRotation(viewFinder.getDisplay().getRotation())
.build();
Preview preview = new Preview(config);
preview.setOnPreviewOutputUpdateListener(previewOutput -> {
ViewGroup parent = (ViewGroup) viewFinder.getParent();
parent.removeView(viewFinder);
parent.addView(viewFinder, 0);
As well as other use case that we can keep empty:
final ImageAnalysisConfig imageAnalysisConfig =
new ImageAnalysisConfig.Builder()
.setLensFacing(LENS_FACING)
.setTargetResolution(new Size(224, 224))
.setCallbackHandler(new Handler(inferenceThread.getLooper()))
.setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
.build();
final ImageAnalysis imageAnalysis2 = new ImageAnalysis(imageAnalysisConfig);
imageAnalysis2.setAnalyzer((image, rotationDegrees) -> { });
New we have a line to bring them to life:
CameraX.bindToLifecycle(this, preview, imageAnalysis2);
And here is where the problem starts. If we keep them like that the application is unusable. But if we keep only one (either one) we can use the app to the point that all novigation works normally. Do you know what is causing this behaviour?
This build.gradle uses a rather old version; migrate to version 1.0.0-beta01.

Why do I need to add space before = in Kotlin?

In the following code , I have to add space before = , why ?
I think private val listofMDetail: MutableList<MDetail>= (No space before =) would be OK, but in fact, the Android Studio 3.1.3 give me an error.
private val listofMDetail: MutableList<MDetail> = //I have to add space before =
try{
myGson.fromJson<MutableList<MDetail>>(mJson)?: mutableListOf<MDetail>() //Load
}catch(e:Exception) {
e.message?.let{ logError("Paser: "+it)}
throw Exception(e)
}
The symbol >= is read as greater than or equal to, that is why the compiler show the error Expecting a '>'.
Just add the space, as recommended in the Kotlin style guide maintained by Google: https://android.github.io/kotlin-guides/style.html

Categories

Resources