旋转图像文件而不更改文件大小
我想旋转图像文件(照片)而不更改文件大小:
- 我使用 Bitmap.decodeFile 创建位图
我应用旋转:
矩阵矩阵 = new Matrix(); 矩阵.postRotate(90); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), 矩阵, true);
我将位图保存到文件中:
bitmap.compress(CompressFormat.JPEG, 100, Stream);
但原始文件的大小增加了一倍!
I want to rotate an image file (photo) without changing file size :
- I create a bitmap with Bitmap.decodeFile
I apply the rotation :
Matrix matrix = new Matrix(); matrix.postRotate(90); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
I save bitmap into a file with :
bitmap.compress(CompressFormat.JPEG, 100, stream);
but the size of my original file has doubled !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您加载的图像以特定格式(例如 JPEG)和特定压缩质量进行编码(质量越高,文件大小越大)。当您在旋转后保存它时,您指定了可能完全不同的格式和压缩质量,因此存在大小差异。
我认为没有任何方法可以保证相同的大小,除非输入和输出文件都是未压缩的位图,但您可能至少可以尝试找出原始图像使用的格式和质量并使用这些设置当您使用 bitmap.compress 再次保存文件时。这至少应该给你一个大致相同的文件大小。
The image you load is encoded in a certain format (e.g. JPEG) and with a certain compression quality (where better quality equals larger file size). When you save it after rotating, you are specifying a potentially completely different format and compression quality, hence the size difference.
I don't think there is any way you can guarantee the same size unless both the input and output files are uncompressed bitmaps, but you can probably at least try to figure out what format and quality was used for the original image and use those settings when you save the file again using bitmap.compress . That should give you a file size in the same ballpark at least.
除了压缩格式之外,尝试使用与 src 图像相同的配置创建位图。我建议使用 RGB_565: http://developer.android.com/reference /android/graphics/Bitmap.Config.html
这将产生比默认值更好的值。
现在,如果您将任何想法渲染到 mCanvas,它将保存在位图中:
现在 mBitmap 应该拥有您需要的所有内容(以及大小)。
Apart from the comppression format try to create the Bitmap with the same config as the src image. I would suggest a RGB_565: http://developer.android.com/reference/android/graphics/Bitmap.Config.html
This would result in a better values as the default one.
Now if you render any think to mCanvas it will be saved in the bitmap:
Now mBitmap should have all you need (and the size).