我刚刚完成了我的相机活动,它奇妙地保存数据.
拍照后我做了什么
拍照后我做了什么
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,this.lastCamData.length); Matrix m = new Matrix(); m.postRotate(90); thePicture = Bitmap.createBitmap(thePicture,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();
解决方法
在创建FileOutputStream之前,您可以从使用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