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

Mar*_*kus 14 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);
    }
Run Code Online (Sandbox Code Playgroud)

use*_*055 5

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

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);
Run Code Online (Sandbox Code Playgroud)

  • 请阅读问题 - 我需要旋转字节数组而不将其解码为位图。 (7认同)

use*_*123 2

我认为不存在这种可能性。字节顺序取决于图片编码(png、jpeg)。因此,您被迫解码图像以对其执行某些操作。