[英]Problem with Width and Height properity of a image on java
I'm having a problem with the Width and Height Property of an image on java.我在 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.图像是在另一个软件上生成的,这是一个用于获取口腔内 X 射线的 sdk。
And the sdk code to save the image is that:保存图像的sdk代码是:
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);
如何建议我尝试使用
feredImage bufferedImage = ImageIO.read(file);
instead of Image img = new Image("file:" + file.getPath());
而不是
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)根据要求,这就是我要加载的图像: https : //shortest.link/KFQ (超过 2 兆字节)
The problem is that I'm using Java 8, so to load TIFF images we need a plugin.问题是我使用的是 Java 8,所以要加载 TIFF 图像,我们需要一个插件。
Add these lines to your pom.xml:将这些行添加到您的 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如果您需要使用 jar 或其他存储库,请在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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.