Question

I have a picture (bitmap) and I want to draw some shapes and rotated text on it.

This works fine as long as the picture doesn't get too large. However, when using a picture (2560 x 1920 pixels)taken with the build-in camera of my android 2.1 phone, the result is distorted. It looks like the rotation back, after drawing the rotated text, has not been completed. Also, the distortion point is not always the same, like it depends on the cpu usage.

You can see some resulting pictures here:
http://dl.dropbox.com/u/4751612/Result1.png
http://dl.dropbox.com/u/4751612/Result2.png

The code is executed inside a AsyncTask. The strange this is that this code works fine in one Activity, but not in another. In both activities the AsyncTask is executed when a button is clicked.

These are some excerpts of the code I'm using.

// Load the image from the MediaStore
c = MediaStore.Images.Media.query(context.getContentResolver(),
Uri.parse(drawing.fullImage), new String[] {MediaColumns.DATA});
if (c != null && c.moveToFirst()) {
       imageFilePath = c.getString(0);
       bitmap = ImageUtil.getBitmap(new File(imageFilePath), 10000);
}
c.close();

// Create a canvas to draw on
drawingBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(drawingBitmap);

// Draw image
canvas.drawBitmap(bitmap, 0, 0,
MeasureFactory.getMeasurePaint(context));

// calculate text width
rect = new Rect();
paint.getTextBounds(text, 0, text.length(), rect);

// Draw rotated text
canvas.save();
canvas.rotate(-angle, centerPoint.x, centerPoint.y);
canvas.drawText(text, centerPoint.x-Math.abs(rect.exactCenterX()),
Math.abs(centerPoint.y-rect.exactCenterY()), paint);
canvas.restore();

// Upload the bitmap to the Media Library
Uri uri =
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outStream = getContentResolver().openOutputStream(uri);
drawingBitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
outStream.flush();
outStream.close();

Thanks in advance for any help.

Was it helpful?

Solution

Since it works as long as the resolution isn't too high, I would just rescale all images to something that works.

You can accomplish this using
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 800 /* width */, 600 /* height */, true);

OTHER TIPS

This turned out to be a memory problem although no OutOfMemoryException was visible in the log.

So, I "solved" it by scaling the image if the resolution is too high, as suggested by ingo. The problem is that I don't know how to determine the limits of a device. I suppose they are different for every device and depends on the current memory usage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top