将此有关BufferedImage的代码解释为android Bitmap

时间:2019-06-01 01:28:00

标签: java android bufferedimage

我正在尝试将此代码移植到android库 如您所知,bufferedImage不属于android

我尝试使用位图,但我认为我并没有这样做

DoubleMap readImage(final byte[] serialized) {
    Bitmap buffered = BitmapFactory.decodeByteArray(serialized, 0, serialized.length);
    if (buffered == null)
        throw new IllegalArgumentException("Unsupported image format");
    int width = buffered.getWidth();
    int height = buffered.getHeight();
    int[] pixels = new int[width * height];

    DoubleMap map = new DoubleMap(width, height);
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
             // int pixel = pixels[y * width + x];
            int pixel = buffered.getPixel(x, y);
            int color = (pixel & 0xff) + ((pixel >> 8) & 0xff) + ((pixel >> 16) & 0xff);
            map.set(x, y, 1 - color * (1.0 / (3.0 * 255.0)));

        }
    }
    transparency.logDecodedImage(map);
    return map;
}

DoubleMap readImage(final byte[] serialized) {
        BufferedImage buffered = Exceptions.sneak().get(new ThrowingSupplier<BufferedImage>() {
            @Override
            public BufferedImage get() throws Exception {
                return ImageIO.read(new ByteArrayInputStream(serialized));
            }
        });
        if (buffered == null)
            throw new IllegalArgumentException("Unsupported image format");
        int width = buffered.getWidth();
        int height = buffered.getHeight();
        int[] pixels = new int[width * height];
        buffered.getRGB(0, 0, width, height, pixels, 0, width);
        DoubleMap map = new DoubleMap(width, height);
        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                int pixel = pixels[y * width + x];
                int color = (pixel & 0xff) + ((pixel >> 8) & 0xff) + ((pixel >> 16) & 0xff);
                map.set(x, y, 1 - color * (1.0 / (3.0 * 255.0)));
            }
        }
        transparency.logDecodedImage(map);
        return map;
    }

在android上尝试库时,结果与桌面不同 我相信我做错了什么,但不确定在哪里以及如何修复

0 个答案:

没有答案