Java的imageIO读取图片后,怎么把他显示在panel上呢?

如题所述

您好:读取bmp文件到BufferedImage中

File file2 = new File("c:\\testimages\\tttt" + ".bmp");
// BufferedImage bi = backstore.getBufferedImage();
try {
output = ImageIO.read(file2);
} catch (IOException e) {
e.printStackTrace();
}

输出bmp文件

File file2 = new File("c:\\testimages\\tttt" + ".bmp");
ImageIO.write(bi, "bmp", file2);

Byte[]输出到文件

byte[] buf =UtilZip.zipObjectToByte(cache);
File file2 = new File("c:\\testimages\\cache.bin");
FileOutputStream fos =new FileOutputStream(file2);
fos.write(buf);
fos.flush();
fos.close();

读文件到Byte[]中

File file2 = new File("c:\\testimages\\cache.bin");
FileInputStream fis = new FileInputStream(file2);
byte[] buf = new byte[(int) file2.length()];
fis.read(buf);
fis.close();

填充颜色到整个画布

BufferedImage bi = backstore.getBufferedImage();
Graphics g2 = bi.getGraphics();
g2.setColor(Color.red);
g2.fillRect(0, 0, Common.width,
Common.height);

图像变灰操作

public finalBufferedImage getGrayPicture(BufferedImage originalPic) {
int imageWidth = originalPic.getWidth();
int imageHeight = originalPic.getHeight();

BufferedImage newPic = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_3BYTE_BGR);

ColorConvertOp cco = new ColorConvertOp(ColorSpace
.getInstance(ColorSpace.CS_GRAY), null);
cco.filter(originalPic, newPic);
return newPic;
}

ImageIO
javax.imageio.ImageIO lets you save and restore Images to disk in a platform independent format. It works using plug-in modules that handle various formats including "gif", "png" and "jpeg" (all lower case, or all upper case, but not mixed). "jpeg" or "jpg" is acceptable. Use ImageIO. getWriterFormatNames() to find out which types are supported on your platform:

import javax.imageio.ImageIO;

public class Jai
{

public static void main ( String[] args )
{
String[] names = ImageIO.getWriterFormatNames();
for ( String name: names )
{
System.out.println( name );
}
}
}

Loading an Image from raw bytes
Here raw bytes represent the image of a *.gif, *.png or

// raw bytes to BufferedImage
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;
...
BufferedImage image = ImageIO.read ( new ByteArrayInputStream ( rawImageBytes ) );

Saving an Image to raw bytes
// BufferedImage to raw bytes
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
...

// O P E N
ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 );

// W R I T E
ImageIO.write( aBufferedImage, "jpeg" ,
baos );

// C L O S E
baos.flush();
byte[] resultImageAsRawBytes = baos.toByteArray();

baos.close();

Loading a BufferedImage from a file
// file to BufferedImage
import java.awt.image. BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
...
BufferedImage image = ImageIO.read( new File( "rabbit.jpg" ) );
Saving a BufferedImage to a file
// BufferedImage to File
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
...
ImageIO.write( aBufferedImage, "jpeg" ,
new File ( "snap.jpg" ) );

ImageWriteParam is a way of controlling exactly how the image in encoded. There is currently no PNG support for it. This is not for injecting meta info.
Loading a BufferedImage from an URL
// url to BufferedImage
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
...
BufferedImage image = null;
try
{
image = ImageIO.read( url );
}
catch ( IOException e )
{
System.out.println( "image missing" );
}

Converting Image to BufferedImage
// Image to BufferedImage
import java.awt.image.BufferedImage;
import java.awt.Image;
...
BufferedImage bufferedImage = new BufferedImage ( imageWidth,
imageHeight,
BufferedImage.TYPE_INT_BGR );
bufferedImage.createGraphics().drawImage( image, 0, 0, this );
温馨提示:内容为网友见解,仅供参考
无其他回答

Javase中要显示一个图片,但是图片尺寸太大了,如何让图片能完全显示在规...
大致就是先通过 Java Advanced Image IO (JAI, 在 Oracle 上下载) 把图片读取到内存,然后缩放后设置成 ImageIcon 显示到一个 Panel 上。直接让 Swing 自己读取图片时好像不会自动缩放的。FileInputStream input = new FileInputStream(path);BufferedImage image = ImageIO.read(input);image = scal...

使用ImageIO将BufferedImage载入到JFrame中,用什么办法?
import java.awt.Container;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.FileInputStream;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JPanel;public class BufferedImageTest{ public static void main(String[] args){ JFrame f = ...

通过对话框 向JPanel中导入 图片
http:\/\/piscesky.javaeye.com\/blog\/281952首先我也是菜鸟,楼主可以等好点的答案,这个是网络的图像I\/O库的用法,下面的例子使用JFileChooser提示输入图像文件名。选中文件后再选择目标输出格式,然后按下“Save(保存)”按钮。保存完成后,将重新读取图像并在一个新窗口内显示。import java.awt.*;import...

请问java的JLabel上的图片如何刷新?
background = new ImageIcon("D:\/\/ClientData\/\/result.jpg");resultLabel.setIcon(background);resultLabel.repaint();resultLabel.updateUI();resultLabel.setVisible(true);试一下这种方法:String path = "D:\/\/ClientData\/\/result.jpg";Icon icon;try { icon = new ImageIcon(ImageIO.read(new ...

java中如何得到屏幕图像信息并保存起来 最后画出来 是不是用BufferedIm...
3.重新画出图片:第二步中已经把图片以对象的形式保存在文件中了,那么你要画出来就需要重新从文件中读取出这个图片的信息(IO上的知识)(x,y,width,height...),Frame会自动调用paint方法,但是你需要在Image的Bean里面定义它自身的draw方法,然后在paint方法中调用draw方法,这样就可以在你的程序窗...

JAVA中image(用Bufferedinmage得到的image)所在的frame干扰其他frame...
我想你的问题应该是没有覆盖public void repaint()方法,你可以实现这个方法,然后在先调用 g.clearRect(int x, int y, int width, int height)方法来清楚整个屏幕,然后在其中调用this.paint()方法。关于图片的大小设置问题,你可以设置为一个动态的值,假设你的frame的长x,宽y,那么可以设置为...

Java程序中图片的使用方法
ImageIcon(String filename)或new ImageIcon(URL location)都可以直接加载图像文件。甚至你还可以先用IO读出byte流,也能用byte[]创建ImageIcon。还可以由已有的Image来创建。更为高级的用法是,你还可以先创建个空的ImageIcon,然后重写paintIcon(Component,Graphics,int,int)方法实现手工绘制!如果你是要完成...

java中怎么设置大小图片?
具体步骤如下所示:1、打开电脑,进入软件中,编写设置大小的代码,代码如下所示。2、在左边的选项中选择javastudy包,在子文件下选择Java。3、选择之后,就会看到以下画面,通过代码加入图片,代码如下图所示。4、写好代码之后,选择左边的菜单,如下图所示,点击以下选项。5、图片插入之后,就成功了。

...开文件选择器JFileChooser,在JPanel上显示所显示的图片
import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.filechooser.FileNameExtensionFilter;public class MyPanel extends JPanel{private static final long serialVersionUID = 1L;BufferedImage bi = null;public MyPanel (){final JFileChooser chooser = new JFileChooser ();File...

java bufferedimage
如果Java想将一副图片加载到内存中的方法是:BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));那么如果想编写一个网络程序,通过Socket将图片从服务器端传到客户端,并存入文件系统,该如何操作?示例如下: Server端: package org.exam3; import java.awt....

相似回答