【问题标题】:How rotate picture from "onPictureTaken" without Out of memory Exception?如何在没有内存不足异常的情况下从“onPictureTaken”旋转图片?
【发布时间】:2012-08-06 16:45:43
【问题描述】:

我在那里读了很多帖子?但我没有找到正确的答案。

我尝试这样做:

@Override
public void onPictureTaken(byte[] paramArrayOfByte, Camera paramCamera) {
    try {


        Bitmap bitmap = BitmapFactory.decodeByteArray(paramArrayOfByte, 0,
        paramArrayOfByte.length);

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        FileOutputStream os = new ileOutputStream(Singleton.mPushFilePath);

        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
            height, matrix, false);

        resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 95, os);
        os.close();
        ...

有没有办法在不使用 BitmapFactory 的情况下旋转图片?我想旋转图片而不损失质量!

【问题讨论】:

标签: android bitmap bitmapfactory


【解决方案1】:

也许您可以使用 Camera.setDisplayOrientation 拍摄已旋转的图片?检查Android camera rotate。此外,调查 Camera.Parameters.setRotation()。其中一种技术应该可以为您解决问题。

否则您的代码看起来很好,除了在 Bitmap.compress 上使用参数 95,您需要使用 100 进行无损压缩。

为避免内存不足异常,请使用 Camera.Parameters.setPictureSize() 拍摄较低分辨率的图片(例如 3Mpx)。即你真的需要一张 8Mpx 的照片吗?确保使用 Camera.Parameters.getSupportedPictureSizes() 来确定您设备上支持的尺寸。

【讨论】:

  • 很遗憾,setDisplayOrientaion() 仅适用于预览,不会影响在onPicturetaken() 中捕获的缓冲区。实际解决方案请见stackoverflow.com/a/18447809/192373
  • setRotation 就是这样做的——旋转您在 onPictureTaken 中收到的实际图像。
猜你喜欢
  • 2014-07-25
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多