'Android Java - how to clear repeat drawings?
I have this code where I draw on a canvas then convert to a bitmap.
predictBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//if image is empty then let the user know to upload one
if(img == null){
Toast.makeText(CameraActivity.this, "Upload an image first!", Toast.LENGTH_SHORT).show();
} // otherwise run the custom tensorflow lite model
else {
imageBox.destroyDrawingCache();
img = Bitmap.createScaledBitmap(img, 281, 324, true);
try {
Android model = Android.newInstance(getApplicationContext());
// Creates inputs for reference.
TensorImage image = TensorImage.fromBitmap(img);
// Runs model inference and gets result.
Android.Outputs outputs = model.process(image);
Android.DetectionResult detectionResult = outputs.getDetectionResultList().get(0);
// Gets result from DetectionResult.
float score = detectionResult.getScoreAsFloat();
RectF location = detectionResult.getLocationAsRectF();
String category = detectionResult.getCategoryAsString();
//Create a new canvas to draw on
Canvas canvas = new Canvas(img);
//draw on the canvas with the given location from the model
drawBoundingBox(canvas, location);
// Releases model resources if no longer used.
model.close();
// here we will print out the results of the object to text views based on the image that is inputted by the user
// we print out object type, accuracy score and location of object on the image
objecttv.setText(category);
scoretv.setText(Float.toString(score));
textBox.setText(newText);
imageBox.setImageBitmap(img);
} catch (IOException e) {
//print out an error to the user and restart the activity.
Toast.makeText(CameraActivity.this, "An error happened " + e, Toast.LENGTH_SHORT).show();
sameActivity();
}
}// end of else
}
}
My code for drawBoundingBox
void drawBoundingBox(Canvas canvas, RectF location) {
//calling invalidate() here gives me a cannot find symbol
Paint boxPaint = new Paint();
boxPaint.setColor(Color.RED);
boxPaint.setAlpha(200);
boxPaint.setStyle(Paint.Style.STROKE);
canvas.drawRect(location, boxPaint);
}
Everytime I run the predict button it draws a new rectangle location but keeps the old location .
I've tried the following
imageBox.setImageDrawable(null);
imageBox.destroyDrawingCache();
Here is how the image looks each time.
None of these clear my drawings , what else can I try?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|