简体   繁体   English

将位图旋转90度并保存

[英]Rotating a bitmap 90 degrees and saving

all! 所有! I'm at a total loss here with this question. 我在这个问题上完全不知所措。 It may have been answered, but I can't find anyone asking for precisely what I need. 可能已经回答了,但是我找不到有人确切地问我需要什么。

I have a jpeg file saved to the SDCARD, and want to programatically rotate it 90 degrees clockwise, then save it back to the card (either overwriting the original or deleting the original after editing). 我有一个jpeg文件保存到SDCARD,并希望以编程方式将其顺时针旋转90度,然后将其保存回卡中(覆盖原始文件或在编辑后删除原始文件)。 The jpeg is loaded into jpeg已加载到

File myFile;

Can anyone advise me as to how to go about this? 谁能建议我该怎么做? I imagine it has something to do with BitmapFactory and Matrices, but like I said: I'm a little over my head here! 我想这与BitmapFactory和Matrices有关,但是就像我说的那样:我有点不高兴了!

Thanks! 谢谢!

Try this. 尝试这个。

public Bitmap rotate(String file)
{
         Bitmap source = BitmapFactory.decodeFile("/sdcard/ChuckNorris.png");
         Bitmap b = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Config.ARGB_8888);

         Paint p = new Paint();
         p.setColor(Color.BLACK);

         Canvas f = new Canvas(b);
         f.drawBitmap(source, 0, 0, p);

         f.rotate(90);

         return b;
}

Note: Doesn't try on my own 注意:请勿自行尝试

I found the answer! 我找到了答案!

For JPG files, apparently you can just niftily edit the exif data using the following method: 对于JPG文件,显然,您可以使用以下方法轻松编辑exif数据:

ExifInterface exif = new ExifInterface(myFile.getAbsolutePath());
exif.setAttribute(ExifInterface.TAG_ORIENTATION, "6");
exif.saveAttributes();

Are there any downsides to using this approach? 使用这种方法是否有不利之处? It seems to work pretty well as far as I can determine.. 据我所知,它似乎工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM