无法在特定图片上施放Databufferint到Databufferbyte

发布于 2025-01-29 20:43:04 字数 2357 浏览 2 评论 0原文

我正在尝试提高图像的阈值,但是其中一些人在铸造到Databufferbyte时有问题。我在这里发布了两种类型的图片 - 第一张图片(test1.jpg很好,可以铸造到databufferbyte),第二个图片(test2.jpg抛出例外)。

图像:

在此处输入图像描述“

”在此处输入图像说明“

这是我的代码:

 public static void main(String[] args) throws IOException {

    System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
    BufferedImage bufferedImage = ImageIO.read(new File("/test/test2.jpg"));
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
    byte[] input = byteArrayOutputStream.toByteArray();
    InputStream is = new ByteArrayInputStream(input);
    bufferedImage = ImageIO.read(is);
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
    Mat mat = new Mat(bufferedImage.getHeight(), bufferedImage.getWidth(), CvType.CV_8UC3);
    mat.put(0, 0, data);
    Mat mat1 = new Mat(bufferedImage.getHeight(),bufferedImage.getWidth(),CvType.CV_8UC1);
    Imgproc.cvtColor(mat, mat1, Imgproc.COLOR_RGB2GRAY);
    byte[] data1 = new byte[mat1.rows() * mat1.cols() * (int)(mat1.elemSize())];
    mat1.get(0, 0, data1);
    Mat dst = new Mat();
    Imgproc.adaptiveThreshold(mat1, dst, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 11, 15);
    MatOfByte matOfByte = new MatOfByte();
    Imgcodecs.imencode(".jpg", dst, matOfByte);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(matOfByte.toArray());
    bufferedImage = ImageIO.read(inputStream);
    ImageIO.write(bufferedImage, "jpg", new File("/test_output/test2.jpg"));

}

test1.jpg映像的输出正确:

线程“ main” java.lang.classcastException中的例外: java.awt.image.databufferint不能施放 java.awt.image.databufferbyte

在线

byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

有什么问题?为什么将.jpg图像转换而毫无问题,但第二次抛出了例外?

I am trying to threshold my images, but some of them have problem with casting to DataBufferByte. I post here two types of pictures - the first (the test1.jpg is good and can be casted to DataBufferByte), the second (test2.jpg throwing an exception).

Images:

enter image description here

enter image description here

Here is my code:

 public static void main(String[] args) throws IOException {

    System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
    BufferedImage bufferedImage = ImageIO.read(new File("/test/test2.jpg"));
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
    byte[] input = byteArrayOutputStream.toByteArray();
    InputStream is = new ByteArrayInputStream(input);
    bufferedImage = ImageIO.read(is);
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
    Mat mat = new Mat(bufferedImage.getHeight(), bufferedImage.getWidth(), CvType.CV_8UC3);
    mat.put(0, 0, data);
    Mat mat1 = new Mat(bufferedImage.getHeight(),bufferedImage.getWidth(),CvType.CV_8UC1);
    Imgproc.cvtColor(mat, mat1, Imgproc.COLOR_RGB2GRAY);
    byte[] data1 = new byte[mat1.rows() * mat1.cols() * (int)(mat1.elemSize())];
    mat1.get(0, 0, data1);
    Mat dst = new Mat();
    Imgproc.adaptiveThreshold(mat1, dst, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 11, 15);
    MatOfByte matOfByte = new MatOfByte();
    Imgcodecs.imencode(".jpg", dst, matOfByte);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(matOfByte.toArray());
    bufferedImage = ImageIO.read(inputStream);
    ImageIO.write(bufferedImage, "jpg", new File("/test_output/test2.jpg"));

}

The output for test1.jpg image is correct:

enter image description here

But the second image throwing an exception

Exception in thread "main" java.lang.ClassCastException:
java.awt.image.DataBufferInt cannot be cast to
java.awt.image.DataBufferByte

at line

byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

What is the problem ? Why the one .jpg image is converted without problems but second throwing an exception?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栩栩如生 2025-02-05 20:43:04

两者都不具有相同的格式。一个使用8位用于颜色表示和其他3x8位(SO 4个字节,带有填充或透明度)?

查看它给出:

  • image1:JPEG图像数据,JFIF标准1.01,纵横比,密度1x1,段长度16,基线,Precision 8,449x361,组件3

  • image2:PNG图像数据,617 x 353,8位/彩色RGBA,非颜色RGBA,非 - 间隔

您可以在缓冲区上使用getDatatype()方法来获取颜色表示的大小。

Both are not of the same format. One using 8-bit for color representation and the other 3x8-bit (so 4 bytes, with padding or transparency)?

Looking at it give:

  • image1 : JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 449x361, components 3

  • image2 : PNG image data, 617 x 353, 8-bit/color RGBA, non-interlaced

You may use the getDataType() method on the buffer to get the size of the color representation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文