2012-03-07 103 views
9

我刚完成我的相机活动,它很奇妙地保存数据。 我做什么拍摄照片后:Android在保存前旋转图片

protected void savePictureData() { 
    try { 
     FileOutputStream fs = new FileOutputStream(this.photo); 
     fs.write(this.lastCamData); 
     fs.close(); //okay, wonderful! file is just written to the sdcard 

     //--------------------- 
     //--------------------- 
     //TODO in here: dont save just the file but ROTATE the image and then save it! 
     //--------------------- 
     //--------------------- 


     Intent data = new Intent(); //just a simple intent returning some data... 
     data.putExtra("picture_name", this.fname); 
     data.putExtra("byte_data", this.lastCamData); 
     this.setResult(SAVED_TOOK_PICTURE, data); 
     this.finish(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     this.IOError(); 
    } 

} 

我想什么是已经在上面的代码中给出评论。我不想要图像只是保存到文件,但要旋转,然后保存!谢谢!

// 编辑:我现在什么最多(工程,但仍然运行与大图像内存问题)

byte[] pictureBytes; 
Bitmap thePicture = BitmapFactory.decodeByteArray(this.lastCamData, 0, this.lastCamData.length); 
Matrix m = new Matrix(); 
m.postRotate(90); 
thePicture = Bitmap.createBitmap(thePicture, 0, 0, thePicture.getWidth(), thePicture.getHeight(), m, true); 

ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
thePicture.compress(CompressFormat.JPEG, 100, bos); 
pictureBytes = bos.toByteArray(); 

FileOutputStream fs = new FileOutputStream(this.photo); 
fs.write(pictureBytes); 
fs.close(); 
Intent data = new Intent(); 
data.putExtra("picture_name", this.fname); 
data.putExtra("byte_data", pictureBytes); 
this.setResult(SAVED_TOOK_PICTURE, data); 
this.finish(); 
+0

的可能重复[安卓:如何在一个中心点旋转一个位图(http://stackoverflow.com/questions/4166917/android-how-to-rotate-a-bitmap-on-a - 中心点) – slayton 2012-03-07 17:21:10

+0

不,真的,它更多关于如何旋转和保存之后 – androidavid 2012-03-07 17:49:00

+0

好吧我的解决方案似乎工作 - 清理项目... – androidavid 2012-03-07 23:24:25

回答

4

之前您创建FileOutputStream您可以创建从原来的一个新的Bitmap已使用Matrix进行了转换。要做到这一点,你可以使用这个方法:

createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) 

m定义了一个矩阵,将转你的原始位。

关于如何在这个问题做一下这个例子: Android: How to rotate a bitmap on a center point

+0

嘿,看看我的编辑。我试图但它不能像这样工作...:/ – androidavid 2012-03-07 17:33:54

+0

我正在尝试这样的事情。但每次我复制位图时,我得到一个异常:内存不足... – 2013-01-30 00:30:16

+0

@JoubertVasconcelos,那么你需要缩小图像。 – slayton 2013-01-30 01:53:03

17

你可以只是读从SD卡中的路径,并执行以下代码...它会转动它后,更换现有的照片..

注意:Exif不适用于大多数设备,它提供了错误的数据,所以在保存到任何程度之前硬编码旋转是很好的,您只需将postRotate中的角度值更改为任何你想要的。

String photopath = tempphoto.getPath().toString(); 
    Bitmap bmp = BitmapFactory.decodeFile(photopath); 

    Matrix matrix = new Matrix(); 
    matrix.postRotate(90); 
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 

    FileOutputStream fOut; 
    try { 
     fOut = new FileOutputStream(tempphoto); 
     bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut); 
     fOut.flush(); 
     fOut.close(); 

    } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
+0

这对我有用。谢谢。 – BRS 2014-12-16 07:06:22

+0

伟大的老板,也为我工作! – 2015-02-05 05:14:03

+0

我们让程序员使用照片的问题来自位图类!总是,您将位图类的图像传递给90度旋转照片。然后解决方案是,我们总是使用位图类,我们需要显示de照片,我们需要应用'matrix.postRotate(90);'创建一个旋转的位图。 – 2015-06-10 15:14:20