Android - 大位图上的旋转文本

发布于 2024-10-18 22:40:44 字数 1849 浏览 3 评论 0原文

我有一张图片(位图),我想绘制一些形状并旋转 上面有文字。

只要图片不会变得太大,这种方法就可以正常工作。然而, 使用内置拍摄的图片(2560 x 1920 像素)时 我的 android 2.1 手机的相机,结果失真。 看起来旋转回来了,在绘制旋转的文本之后,已经 尚未完成。此外,畸变点并不总是相同, 就像它取决于CPU使用率一样。

您可以在此处查看一些生成的图片:
http://dl.dropbox.com/u/4751612/Result1.png
http://dl.dropbox.com/u/4751612/Result2.png

该代码在 AsyncTask 内执行。奇怪的是,这段代码在一个 Activity 中运行良好,但在另一个 Activity 中则不然。在这两个活动中,单击按钮时都会执行 AsyncTask。

这些是我正在使用的代码的一些摘录。

// 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();

预先感谢您的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈独秀 2024-10-25 22:40:44

由于只要分辨率不太高它就可以工作,因此我只需将所有图像重新调整为可以工作的尺寸。

您可以使用
来完成此操作
BitmapscaledBitmap = Bitmap.createScaledBitmap(bitmap, 800 /* 宽度 */, 600 /* 高度 */, true);

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);

世界如花海般美丽 2024-10-25 22:40:44

尽管日志中没有显示 OutOfMemoryException,但事实证明这是一个内存问题。

因此,按照 ingo 的建议,如果分辨率太高,我会通过缩放图像来“解决”这个问题。问题是我不知道如何确定设备的限制。我想它们对于每个设备都是不同的,并且取决于当前的内存使用情况。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文