I'm having a problem with the Width and Height Property of an image on java. For some reason the are null.
And on the properties of the image, I have the dimensions of the file.
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
As requested, thats the image I'm trying to load: https://shortest.link/KFQ (over 2 Megabytes)