在onPictureTaken之后旋转JPEG的字节数组

时间:2013-05-20 14:34:24

标签: android android-camera

有没有办法旋转字节数组而不将其解码为Bitmap?

目前在jpeg PictureCallback中我只是将字节数组直接写入文件。但图片是旋转的。我想旋转它们而不解码到位图,希望这样可以节省我的记忆。

    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(data, 0, data.length, o);

    int orientation;
    if (o.outHeight < o.outWidth) {
        orientation = 90;
    } else {
        orientation = 0;
    }

    File photo = new File(tmp, "demo.jpeg");

    FileOutputStream fos;
    BufferedOutputStream bos = null;
    try {
        fos = new FileOutputStream(photo);
        bos = new BufferedOutputStream(fos);
        bos.write(data);
        bos.flush();
    } catch (IOException e) {
        Log.e(TAG, "Failed to save photo", e);
    } finally {
        IOUtils.closeQuietly(bos);
    }

3 个答案:

答案 0 :(得分:3)

您可以通过Exif标头设置JPEG旋转而不进行解码。这是最有效的方法,但有些观看者可能仍会显示旋转的图像。

或者,您可以使用JPEG lossless rotation不幸的是,我不知道这种算法的免费Java实现。

在SourceForge上

更新,有一个Java开源类LLJTran。 Android端口位于GitHub

答案 1 :(得分:2)

我认为没有这种可能性。字节顺序取决于图片编码(png,jpeg)。所以你被迫解码图像来做一些事情。

答案 2 :(得分:1)

试试这个。它将解决目的。

Bitmap storedBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, null);

Matrix mat = new Matrix();                        
mat.postRotate("angle");  // angle is the desired angle you wish to rotate            
storedBitmap = Bitmap.createBitmap(storedBitmap, 0, 0, storedBitmap.getWidth(), storedBitmap.getHeight(), mat, true);
相关问题