-2

I'm having a problem with the Width and Height Property of an image on java. For some reason the are null.

Debbug result

And on the properties of the image, I have the dimensions of the file.

Image Properties

I need to control the dimension for not get an exception when the image are displayed on screen. Because I have to calculate the dimensions when zoon in or out and some other stuffs.

This is the code to loading and verificate the image widht and height

if (confirmOverwrite()) {
            FileChooser fileChooser = new FileChooser();
            if (previousImportDir != null) {
                fileChooser.setInitialDirectory(previousImportDir.getParentFile());
            }
            fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Arquivos de Imagem", "*.png", "*.jpg", "*.bmp", "*.gif"));
            fileChooser.setTitle("Importar arquivo de imagem");
            File file = fileChooser.showOpenDialog(stage);
            if (file != null) {
                previousImportDir = file;
                Image img = new Image("file:" + file.getPath());
                if ((img.getWidth() != 0 && img.getHeight() != 0))
                    actionSetNewImageOnCanvas(img);

And the image is generate on another software, that are an sdk to acquire intraoral x-ray.

And the sdk code to save the image is that:

private void SaveImage(short[] data, int widht, int height)
        {
            try
            {
                Bitmap pic = new Bitmap(widht, height, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);

                Rectangle dimension = new Rectangle(0, 0, pic.Width, pic.Height);
                BitmapData picData = pic.LockBits(dimension, ImageLockMode.ReadWrite, pic.PixelFormat);

                IntPtr pixelStartAddress = picData.Scan0;

                //Marshal.Copy(data, 0, pixelStartAddress, data.Length);
                Marshal.Copy(data, 0, pixelStartAddress, data.Length);

                pic.UnlockBits(picData);


                //SaveBmp(pic, Path.Combine(Directory.GetCurrentDirectory(), "imagem"));

                SaveBmp(pic, "C:\\Users\\WIM\\Desktop\\teste\\teste\\teste.bmp");
                pic.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private static void SaveBmp(Bitmap bmp, string path)
        {
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

            BitmapData bitmapData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);

            var pixelFormats = ConvertBmpPixelFormat(bmp.PixelFormat);

            BitmapSource source = BitmapSource.Create(bmp.Width,
                                                      bmp.Height,
                                                      bmp.HorizontalResolution,
                                                      bmp.VerticalResolution,
                                                      pixelFormats,
                                                      null,
                                                      bitmapData.Scan0,
                                                      bitmapData.Stride * bmp.Height,
                                                      bitmapData.Stride);

            bmp.UnlockBits(bitmapData);


            FileStream stream = new FileStream(path, FileMode.Create);

            TiffBitmapEncoder encoder = new TiffBitmapEncoder();

            encoder.Compression = TiffCompressOption.Zip;
            encoder.Frames.Add(BitmapFrame.Create(source));
            encoder.Save(stream);

            stream.Close();
        }


        private static System.Windows.Media.PixelFormat ConvertBmpPixelFormat(System.Drawing.Imaging.PixelFormat pixelformat)
        {
            System.Windows.Media.PixelFormat pixelFormats = System.Windows.Media.PixelFormats.Default;

            switch (pixelformat)
            {
                case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                    pixelFormats = PixelFormats.Bgr32;
                    break;

                case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                    pixelFormats = PixelFormats.Gray8;
                    break;

                case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:
                    pixelFormats = PixelFormats.Gray16;
                    break;
            }

            return pixelFormats;
        }

How suggested I tried to use feredImage bufferedImage = ImageIO.read(file); instead of Image img = new Image("file:" + file.getPath());

But the object are null

debbug Buffered Image

As requested, thats the image I'm trying to load: https://shortest.link/KFQ (over 2 Megabytes)

vaibhavsahu
  • 612
  • 2
  • 7
  • 19
  • Where is your Java code? Why post the C# code of the SDK? Is it relevant? It would be enough to just link a BMP that showcases the problem... Most likely, you are using an old API that is asynchronous, and your image dimensions are not loaded (yet), but that's just a guess as we are missing a lot of details... – Harald K Aug 27 '21 at 15:08
  • The java part is the line of verification. The rest of the code isn't relevant, because that alredy works And how I can open the image on windows I assumed that are some problem on the loading image on java, and the debbung screenshot show that the file is loaded. Or the save image of the sdk was missing something. So i assumed that someone will ask the code of saving image, so it's the reason for the C# code; But I will add the code of importing the image. But all the images works, excluded that from the sdk. – MatheusCandido Aug 27 '21 at 17:44
  • As I guessed, you are using the old synchronous image API. Try changing `Image img = new Image("file:" + file.getPath());` to `BufferedImage img = ImageIO.read(file);`. It will probably work right away, or at least it will tell you why it doesn't. Still, I don't understand why have all that file chooser related code in the question... Focus on the *problem*. Everything else is just noise... – Harald K Aug 30 '21 at 17:00
  • @HaraldK I tried, and the object is null... – MatheusCandido Aug 30 '21 at 17:12
  • That indicates that the file is not recognized by `ImageIO` as a BMP file (read the docs for details). As I mentioned in my first comment, please link a BMP that showcase the problem. Most likely, it is not a valid BMP at all. – Harald K Aug 30 '21 at 17:20
  • PS: What Java version are you on? It seems that your C# code writes a TIFF file with .BMP extension in a method named `SaveBmp`... Don't do such things! Anyway... TIFF support is built into ImageIO from Java 9, for earlier versions you need [a plugin](https://github.com/haraldk/TwelveMonkeys#file-formats-supported). TIFF is not supported in the old async API. – Harald K Aug 30 '21 at 17:34
  • I'm using Java 8. And what I can use instead TIFF? – MatheusCandido Aug 30 '21 at 17:39
  • Ahem... As you save the file with a .BMP extension from a method named `SaveBmp`... Perhaps just use BMP format? Or, you can use PNG format (but just renaming the file as in the link doesn't help, it needs to be PNG format). And yes, unsurprisingly, the file you linked is a TIFF. – Harald K Aug 30 '21 at 17:43
  • I managed to resovle the problem, I use the jai-imageio dependency to load and them I converted the buffered image to the javafx.scene.image. Thanks to mentionate that I need a plugin to load TIFF files. Thats helped a lot. Have another way to save the image? I really don't know much about that things yet. – MatheusCandido Aug 30 '21 at 18:10

1 Answers1

0

The problem is that I'm using Java 8, so to load TIFF images we need a plugin.

Add these lines to your pom.xml:

<dependency>
   <groupId>com.github.jai-imageio</groupId>
   <artifactId>jai-imageio-core</artifactId>
   <version>1.4.0</version>
</dependency>

If you need to use a jar or other repositories see more in github

And on the code do these changes:

FileInputStream fis = new FileInputStream(file);
BufferedImage bufferedImage = ImageIO.read(fis);
System.out.println("Width: " + bufferedImage.getWidth());
Image img = SwingFXUtils.toFXImage(bufferedImage, null);