【发布时间】:2017-01-09 16:49:53
【问题描述】:
我有一个存储为 byte[] 数组的图像,我想在将字节写入其他位置之前垂直翻转图像。
图像字节来自压缩的 jp2 图像文件。我已经研究过实现类似Flip image stored as a byte[] array 的东西,但我不在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;
}
【问题讨论】:
-
那么我们知道什么?
-
@HovercraftFullOfEels 严格来说,在交错它们之前,我在它们自己的数组中有高字节和低字节。几乎所有已知的都是像素数据数组和一个包含原始预压缩头信息的数据数组。
-
"但图像的高度和宽度未知" 这使得无法知道在哪里交换像素(除非高度和宽度是素数,并且你知道哪个更大)。
-
@AndyTurner 这就是我害怕的。好吧,如果不可能,那就不可能了。我会尝试想出一个不同的解决方案来解决我的问题。谢谢!
-
我猜你需要一个库来将 jp2 压缩转换为未压缩的图像。这是否存在,我不知道。