翻转存储为字节数组的图像

Sar*_*rah 5 java arrays byte image

我有一个存储为byte []数组的图像,我想在将字节写入其他地方的磁盘之前垂直翻转图像.

图像字节来自压缩的jp2图像文件.我已经研究过像Flip图像存储为byte []数组这样的东西,但是我没有在android中工作,也没有访问BitmapFactory.我还考虑首先将字节数组转换为BufferedImage,然后翻转它,但是在当前上下文中不知道图像的高度和宽度(编辑:我修改了代码,因此高度和宽度都是现在已知).

有没有办法通过严格的数组操作来做到这一点?

编辑:尝试翻转代码

 public static byte[] flip(byte[] imageBytes) {
    //separate out the sub arrays
    byte[] holder = new byte[imageBytes.length];
    byte[] subArray = new byte[dimWidth];//dimWidth is the image width, or number of matrix columns
    int subCount = 0;
    for (int i = 0; i < imageBytes.length; i++) {
        subArray[subCount] = imageBytes[i];
        subCount++;
        if (i% dimWidth == 0) {
            subArray = reverse(subArray);
            if (i == (dimWidth)) {
                holder = subArray;
            } else {
                holder = concat(holder, subArray);
            }
            subCount = 0;
            subArray = new byte[dimWidth];
        }
    }
    subArray = new byte[dimWidth];
    System.arraycopy(imageBytes, imageBytes.length - dimWidth, subArray, 0, subArray.length);
    holder = concat(holder, subArray);
    imageBytes = holder;
    return imageBytes;
}
Run Code Online (Sandbox Code Playgroud)

Sar*_*rah 1

没关系,伙计们,我已经成功了。我只需在将高字节和低字节交错到最终数组之前翻转它们即可。对于任何与我有同样问题的人,请在交错之前分别对高字节数组和低字节数组使用上述翻转函数。

感谢您的帮助!