gpt4 book ai didi

java - 将合成导出为图像,与屏幕分辨率无关

转载 作者:行者123 更新时间:2023-11-30 07:58:09 24 4
gpt4 key购买 nike

SWT 中有没有办法将合成导出到始终具有相同大小/分辨率的图像?问题是我们的仪表板在不同显示尺寸/分辨率的屏幕上打开时看起来总是不同。现在的问题是,我可以将仪表板导出到具有固定尺寸的图像,并且无论在哪种屏幕尺寸/分辨率上创建它,它看起来总是相同的吗?

目前我们这样做,但正如所说,这取决于创建它的显示器:

Image image = new Image(Display.getCurrent(), this.content.getBounds().width, this.content.getBounds().height);
ImageLoader loader = new ImageLoader();
FileOutputStream fileOutputStream = new FileOutputStream(file);

GC gc = new GC(image);
this.content.print(gc);

gc.dispose();

loader.data = new ImageData[] { image.getImageData() };
loader.save(fileOutputStream, imageFormat);
fileOutputStream.close();

例如,是否有某种方法可以创建具有特定分辨率的虚拟屏幕,该虚拟屏幕实际上并未显示,仅用于导出仪表板?任何帮助或指示将不胜感激。

最佳答案

在我的一个应用程序中,我使用 SWT 创建图形和图表。用户能够将它们导出为他们指定的尺寸和格式的图像。我的解决方案是采用图表合成的 GC 并将其重新绘制到屏幕外的新合成,然后导出新绘制的合成。

这是我的类(class),它完成了这个任务:

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Composite;

/**
* The class used for writing an {@link Composite} to an image file
*
* @author David L. Moffett
*
*/
public class CompositeImageWriter
{
/**
* Redraws the composite to the desired size off-screen and then writes that
* composite as an image to the desired location and in the desired format
*
* @param absolutePath
* the absolute path to the desired output file
* @param compositeToDraw
* the composite to be written to file as an image
* @param width
* the desired width in pixels that the composite should be redrawn
* to
* @param height
* the desired height in pixels that the composite should be redrawn
* to
* @param imageType
* an int representing the type of image that should be written
*/
public static void drawComposite(String absolutePath, Composite compositeToDraw, int width,
int height, int imageType)
{
Image image = new Image(compositeToDraw.getDisplay(), width, height);
GC gc = new GC(image);
int originalWidth = compositeToDraw.getBounds().width;
int originalHeight = compositeToDraw.getBounds().height;
compositeToDraw.setSize(width, height);
compositeToDraw.print(gc);
compositeToDraw.setSize(originalWidth, originalHeight);

ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image.getImageData() };
loader.save(absolutePath, imageType);

image.dispose();
gc.dispose();
}
}

对于您希望始终以特定尺寸导出的情况,只需将宽度和高度参数替换为适当的常量即可。

编辑 1

我应该补充一点,int imageType 参数是相应的 SWT 修饰符(例如:SWT.IMAGE_PNGSWT.IMAGE_JPEGSWT.IMAGE_BMP等...)。

编辑2

我更新了静态引用以动态从compositeToDraw 获取显示。此外,这是我整理的一个使用 CompositeImageWriter 的示例,您可以使用它来调试您的问题:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

public class CompositeToWrite extends Composite
{
private int width, height;

private Label l;

public CompositeToWrite(Composite parent, int style)
{
super(parent, style);
this.setLayout(new GridLayout(1, true));
this.addListener(SWT.Resize, new Listener()
{

@Override
public void handleEvent(Event event)
{
updateText();
}
});

Button b = new Button(this, SWT.NONE);
b.setText("Export as image (500, 500)");
b.addListener(SWT.Selection, new Listener()
{

@Override
public void handleEvent(Event event)
{
CompositeImageWriter.drawComposite("./img/output.png", CompositeToWrite.this, 500, 500,
SWT.IMAGE_PNG);
}
});

l = new Label(this, SWT.CENTER);
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.verticalAlignment = SWT.CENTER;
l.setLayoutData(gd);
updateText();
}

protected void updateText()
{
width = this.getBounds().width;
height = this.getBounds().height;

l.setText("My label is centered in composite (" + width + ", " + height + ")");
}

}

在此示例中,我创建了一个简单的组合,一旦添加到 shell,它将如下所示: Composite to Write

当我单击该按钮时,它将合成的大小调整为 500 x 500 并将调整大小的合成写入文件。结果就是这张图: Image of resized composite

我应该注意,我确实注意到单击按钮时出现复合闪烁,因此这可能不会像我最初建议的那样完全在后台或“屏幕外”发生。

关于java - 将合成导出为图像,与屏幕分辨率无关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32355656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com