我有代码在屏幕上,在单独的画布中生成一对相关的图形.我希望将这两个图像保存为单独的文件.但是当我使用相同的命令来保存它们时,一个正确保存,另一个保存为空白图像(仅背景颜色).
画布之间有一个区别:一个是简单的绘制,而另一个是在屏幕外关联的缓冲图像,因此我可以用鼠标拖动它.
设置此缓冲安排的代码是
public class VolCanvas extends Canvas
{
volCnv.createBufferStrategy(2);
offScreen = volCnv.getBufferStrategy();
if (offScreen != null) ofsg = (Graphics2D) offScreen.getDrawGraphics();
// carry out draw operations with ofsg
.
.
.
ofsg.dispose();
offscreen.show();
}
Run Code Online (Sandbox Code Playgroud)
然后在保存到文件之前,我通过该函数进行画布到图像转换
private BufferedImage canvasToImage(Canvas cnvs)
{
int w = cnvs.getWidth();
int h = cnvs.getHeight();
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage image = new BufferedImage(w,h,type);
Graphics2D g2 = image.createGraphics();
cnvs.paint(g2);
g2.dispose();
return image;
}//canvasToImage
Run Code Online (Sandbox Code Playgroud)
最后,我在下面显示的ActiionListener中进行保存.
public class SaveChart implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
File outFile; …
Run Code Online (Sandbox Code Playgroud) 我正在创建一个GUI,并且是一个相当新的摇摆和awt.我正在尝试创建一个gui,在启动时,将背景设置为图像,然后使用方法创建各种幻灯片.我已经尝试过了,而且我没有附加到代码中,所以我能够同时修改和/或全新的概念.
编辑(2013年9月15日):我在幻灯片中遇到问题,似乎无法让它发挥作用.
这是我目前的代码.
public class MainFrame extends JFrame{
JLabel backgroundL = null;
private JLabel bakckgroundL;
BufferedImage backimg;
Boolean busy;
double width;
double height;
public MainFrame() throws IOException {
initMainframe();
}
public void initMainframe() throws IOException {
//misc setup code, loads a default jpg as background
setTitle("Pemin's Aura");
busy = true;
String backgroundDir = "resources/frame/background.jpg";
backimg = ImageIO.read(new File(backgroundDir));
backgroundL = new JLabel(new ImageIcon(backimg));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
refreshframe();
setVisible(true);
busy = false;
}
public void adjSize() { // the attempted start of a …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 JPEG2000 (.jp2) 图像转换为其他格式(JPEG 或 PNG),因此我尝试使用javax.imageio
package 的write 方法。这适用于其他格式(例如 JPEG 到 PNG),但是当涉及 JPEG2000(或 TIFF)时,它会引发异常。谁能告诉我输入图像的可能格式是什么?
Exception in thread "main" java.lang.IllegalArgumentException: im == null!
at javax.imageio.ImageIO.write(ImageIO.java:1457)
at javax.imageio.ImageIO.write(ImageIO.java:1565)
at decodeincodeimages.AndroidInterface.convertFormat(AndroidInterface.java:199)
at Main_package.Execute.main(Execute.java:69)
Java Result: 1
Run Code Online (Sandbox Code Playgroud)
这是方法:
public static boolean convertFormat(String inputImagePath,
String outputImagePath, String formatName) throws IOException {
FileInputStream inputStream = new FileInputStream(inputImagePath);
FileOutputStream outputStream = new FileOutputStream(outputImagePath);
// reads input image from file
BufferedImage inputImage = ImageIO.read(inputStream);
// writes to the output image in specified format
boolean result = ImageIO.write(inputImage, …
Run Code Online (Sandbox Code Playgroud) 我需要在图像中找到字母.我需要一个帮助来编写一个算法来解码图像.
我把图像转换成了一个ByteArrayOutputStream
但我不知道我用它做了什么.
有我的java代码:
URL url = new URL(urlImg);
WebClient webClient = new WebClient(BrowserVersion.getDefault());
WebRequest reqImg = new WebRequest(url);
reqImg.setHttpMethod(HttpMethod.GET);
InputStream imgStream = webClient.getPage(reqImg).getWebResponse().getContentAsStream();
BufferedImage img = ImageIO.read(imgStream);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(img, "png", out);
Run Code Online (Sandbox Code Playgroud)
依赖关系:net.sourceforge.htmlunit htmlunit 2.15
JDK 1.6.0_43