我想把一组图片用流写在一个自定义文件里,程序里用到的时候从这个文件里"取",能实现么,
高手帮帮忙,分不够可以再加
高手帮帮忙,分不够可以再加
解决方案 »
- 菜鸟求助:一个类想使用两个无继承关系的类,怎么弄
- 怎么很Session timeout时自动显示登入页面
- 费时查询时如何打开新的窗口,swing多线程问题
- classpath的功能
- 如何用纯java程序 实现访问一个网站的两个页面都返回一个session标示(在线等)
- 悲哀!!!java连接不了SQL数据库!!!
- 关于TreeSet 自定义类比较问题
- 使用Graphics2D时QuadCurve(50,50,10,10,100,50)画曲线?
- 连jbuilder7.0里面自带的jdk的demo都不能显示的原因?
- 哪儿有sl275中文版下载?
- 送分:请给我一个下载地址,是结合Java、数据结构、算法三者的电子书就OK!或者讲书发送到我的邮箱...多谢...
- 怎么在一个panel里让当前要画的直线处于最底图层?
ImageIcon icon = new ImageIcon(url); int w = icon.getIconWidth();
int h = icon.getIconHeight();
Image image = icon.getImage();
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
pg.grabPixels();
写入:
BufferedImage image = new BufferedImage();
int[] pixels; //read from stream
image.setRGB(0, 0, w, h, pixels, 0, scansize);
另外,我不想用到ImageIcon,这样的需求能实现么,谢谢啊!!
ImageIcon icon = new ImageIcon(imagePath);
int w = icon.getIconWidth();
int h = icon.getIconHeight();
这时候已经得到ImageIcon了,但是w和h返回的都是-1,是什么原因啊??
int startY,
int w,
int h,
int[] rgbArray,
int offset,
int scansize)还要请教 chongkai() 一下,setRGB没有返回值,这个方法到底做了一件什么事呢,我要把"数据"序列化到磁盘上,这个"数据"到底是哪个呢??
* Sets an array of integer pixels in the default RGB color model
* (TYPE_INT_ARGB) and default sRGB color space,
* into a portion of the image data. Color conversion takes place
* if the default model does not match the image
* <code>ColorModel</code>. There are only 8-bits of precision for
* each color component in the returned data when
* using this method. With a specified coordinate (x, y) in the
* this image, the ARGB pixel can be accessed in this way:
* <pre>
* pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)];
* </pre>
* WARNING: No dithering takes place.
*
* @param startX, startY the starting coordinates
* @param w width of the region
* @param h height of the region
* @param rgbArray the rgb pixels
* @param offset offset into the <code>rgbArray</code>
* @param scansize scanline stride for the <code>rgbArray</code>
* @see #getRGB(int, int)
* @see #getRGB(int, int, int, int, int[], int, int)
*/
public void setRGB(int startX, int startY, int w, int h,
int[] rgbArray, int offset, int scansize) {
int yoff = offset;
int off;
Object pixel = null; for (int y = startY; y < startY+h; y++, yoff+=scansize) {
off = yoff;
for (int x = startX; x < startX+w; x++) {
pixel = colorModel.getDataElements(rgbArray[off++], pixel);
raster.setDataElements(x, y, pixel);
}
}
}//这个存储是做什么呢?
raster.setDataElements(x, y, pixel);WritableRaster,这个对象如何用呢,这是我最终要序列化到磁盘的数据源么??
import java.awt.Image;
import java.awt.image.PixelGrabber;
import java.net.URL;
import java.io.File;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.io.DataInputStream;
import java.io.FileInputStream;public class ImageTest { public static void main(String[] args) {
ImageTest t = new ImageTest(); t.saveToFile(t.getPixels(), "E:/test.png");
} //Log log = Log.getInstance();
public int[] getPixels() {
String imagePath = "/f-20.png";
ImageIcon icon = new ImageIcon(getClass().getResource(imagePath)); //(imagePath);
int[] pixels = null;
int w = icon.getIconWidth();
int h = icon.getIconHeight();
Image image = icon.getImage();
pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
//log.debug("w", w);
//log.debug("h", h);
//log.debug("image.w", image.getWidth(null));
//log.debug("image.h", image.getHeight(null));
//log.debug("image", image);
//log.debug("pixels.length", pixels.length);
try {
pg.grabPixels();
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
return pixels;
} public void saveToFile(int[] data, String filePath) { File f = new File(filePath); try { //ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
DataOutputStream out = new DataOutputStream(new
FileOutputStream(f)); int rows = data.length; //out.writeInt(rows); //写入数组的行数 //写入数组具体值
for (int m = 0; m < rows; m++) {
out.writeInt(data[m]);
}
out.close();
}
catch (IOException ex) {
ex.printStackTrace();
} }
}我是这样写的,可是,文件存到磁盘上,文件比原来大了几乎10倍,而且用任何图片工具都打不开是什么原因呢?