将jpeg转换为十六进制表示形式,以在ZPL代码中的~DG标签中使用

时间:2014-07-07 15:48:26

标签: java image hex bufferedimage zpl

我正在尝试使用BufferedImage将图像(jpeg)转换为ASCII HEX表示。 这背后的想法是我将直接在ZPL代码中使用HEX表示。

我想到的整个过程如下:

  • 将img读作BufferedImage
  • 从BufferedImage
  • 读取RGB值
  • 将读取值转换为1或0(布尔表示)
  • 将布尔表示分组为大小为8(即一个字节)并将其转换为HEX表示。

到目前为止,我已经能够使用以下代码实现前两个步骤。

try {
        BufferedImage img = ImageIO.read(ImageToHex.class.getResourceAsStream("Input.JPG"));

        final byte[] pixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
            System.out.println("Pixels : " + pixels);

        final int width = img.getWidth();
        System.out.println("Width : " + width);

        final int height = img.getHeight();
            System.out.println("Height : " + height);

        int[][] result = new int[height][width];

        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                result[row][col] = img.getRGB(col, row);
            }
        }

        System.out.println("Result: " + result);

} catch (IOException e) {
    e.printStackTrace();
}

不确定如何继续获取输入图像的最终十六进制表示。

注意:有一些斑马特定的库将图像转换为GRF(这是十六进制表示),但截至目前我还没有任何计划使用它们。

感谢对此的任何意见。

0 个答案:

没有答案