‘壹’ java将 jpg图像数据读出来,并用十六进制方式显示在文本文件中
使用ByteArrayInputStream将文件读出来,然后转成byte数组,然后将数组的每一成员转成十六进制输出就好了
‘贰’ 请教如何用Java语言读取jpg图片,并显示
1、获取文件夹的路径 2、得到文件夹中的有图片的名称,可以存到数组或者集合中 3、你再到jsp页面做显示, 4、下面是获取路径和文件名的代码,前台显示的代码自己写 String path = 文件夹路径; String names = ""; try { File f = new File(path)
‘叁’ java imageIO 读取图片问题
试了一下,这张jpg图片中含有大量的元数据(注解数据),结构复杂,导致java读取错误。
可用画图等图片编辑软件“另存为”或者“导出”一下,同时去掉元数据。
就可以被ImageIO读取
‘肆’ 怎么用java从文件中读取图片和写入图片到文件里
首先导入各种需要的包:
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;
读取图片的方法如下:
Image[] array = new Image[10];
Image image = ImageIO.read(new File("d:\\source.gif"));//根据你实际情况改文件路径吧
array[0] = image;
图片读出来了。
如果你有一个Image对象,想把它写入文件可以这样做:
BufferedImage image = ImageIO.read(new File("d:\\source.gif"));
//要想保存这个对象的话你要把image声明为BufferedImage 类型
ImageIO.write(image, "png", new File("f:\\test.png"));
‘伍’ java 怎样从磁盘读取图片文件
用JFileChoose 这个类,用来选择文件 主要代码如下:
JFileChooser f = new JFileChooser(); // 查找文件
f.setFileFilter(new FileNameExtensionFilter("图片文件(*.bmp, *.gif, *.jpg, *.jpeg, *.png)", "bmp", "gif","jpg", "jpeg", "png"));
int rVal = f.showOpenDialog(null);
‘陆’ java怎么限制只能从数据库中读取.jpg文件
读写是两个不同的分支,通常都是分开单独使用的,但是原则就是什么文件的流就要用相应的流进行存储。
可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
BufferedReader bre = null;
try {
String file = "D:/test/test.png";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
System.out.println(str);//原样输出读到的内容
};
备注: 流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。
可以通过“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行存储,举例:
OutputStreamWriter pw = null;//定义一个流
pw = new OutputStreamWriter(new FileOutputStream(“D:/test.png”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例
pw.write("我是要写入到记事本文件的内容");//将要写入文件的内容,可以多次write
pw.close();//关闭流
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。
‘柒’ Java如何读取文件夹中所有图片,并显示出来
说一下思路吧,首先遍历文件夹,找到对应后缀的文件(png,jpg之类的),然后创建Bitmap对象,使用inputStream将文件转成bitmap对象,之后使用imageview或者GLview显示图片即可。
注意对大图进行压缩,结束时图片必须回收处理,bitmap.recycle()否则图片多了内存溢出
‘捌’ java或者js有没有读取.jpg的DPI的方法 求例子
DPI信息在jpg文件中用5个字节表示,直接读取二进制流就可以了
偏移 13 的1个字节:
X和Y的密度单位
units=0:无单位
units=1:点数/英寸
units=2:点数/厘米
偏移15的2字节:
水平方向像素密度
偏移16的2字节:
垂直方向像素密度
‘玖’ 用java怎么读取图片
思路:使用 java.awt.Image包下的Image可以接收图片。读取则使用ImageIO对象。
代码如下:
/**
* 读取图片,首先导入以下的包
*/
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;
/**
* 用Image对象来接收图片
* 路径根据实际情况修改
*/
Image image = ImageIO.read(new File("c:\\1.png"));
System.out.println(image.getSource());
‘拾’ 用java实现读取一个jpg图片,根据4个坐标,把坐标内的图片保存为另一个jpg图片
packagetest.any;
importjava.awt.Rectangle;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.util.Iterator;
importjavax.imageio.ImageIO;
importjavax.imageio.ImageReadParam;
importjavax.imageio.ImageReader;
importjavax.imageio.stream.ImageInputStream;
publicclassCutPicture{
privateStringsrcpath;
privateStringsubpath;
privateStringimageType;
privateintx;
privateinty;
privateintwidth;
privateintheight;
publicCutPicture(){
}
publicCutPicture(Stringsrcpath,intx,inty,intwidth,intheight){
this.srcpath=srcpath;
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
publicintgetHeight(){
returnheight;
}
publicvoidsetHeight(intheight){
this.height=height;
}
publicStringgetSrcpath(){
returnsrcpath;
}
publicvoidsetSrcpath(Stringsrcpath){
this.srcpath=srcpath;
if(srcpath!=null){
this.imageType=srcpath.substring(srcpath.indexOf(".")+1,srcpath.length());
}
}
publicStringgetSubpath(){
returnsubpath;
}
publicvoidsetSubpath(Stringsubpath){
this.subpath=subpath;
}
publicintgetWidth(){
returnwidth;
}
publicvoidsetWidth(intwidth){
this.width=width;
}
publicintgetX(){
returnx;
}
publicvoidsetX(intx){
this.x=x;
}
publicintgetY(){
returny;
}
publicvoidsetY(inty){
this.y=y;
}
publicStringgetImageType(){
returnimageType;
}
publicvoidsetImageType(StringimageType){
this.imageType=imageType;
}
publicvoidcut()throwsIOException{
FileInputStreamis=null;
ImageInputStreamiis=null;
try{
is=newFileInputStream(srcpath);
this.setSrcpath(srcpath);
Iterator<ImageReader>it=ImageIO.getImageReadersByFormatName(this.imageType);
ImageReaderreader=it.next();
iis=ImageIO.createImageInputStream(is);
reader.setInput(iis,true);
ImageReadParamparam=reader.getDefaultReadParam();
Rectanglerect=newRectangle(x,y,width,height);
param.setSourceRegion(rect);
BufferedImagebi=reader.read(0,param);
ImageIO.write(bi,this.imageType,newFile(subpath));
}finally{
if(is!=null)
is.close();
if(iis!=null)
iis.close();
}
}
publicstaticvoidmain(String[]args){
CutPictureo=newCutPicture("c:\粗切编目层详细1.png",0,0,100,100);
o.setSubpath("c:\1.png");
try{
o.cut();
}catch(IOExceptione){
e.printStackTrace();
}
}
}