读取一张图片。返回byte[]数组

wei_wxx 2010-06-09 11:21:49
我就是想要这张图片的数据流。要代码。带注释更好。
...全文
880 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
wei_wxx 2010-06-09
  • 打赏
  • 举报
回复
这答案是我写的。大哥。。。
tianbusheng 2010-06-09
  • 打赏
  • 举报
回复
上面这么多答案挨个盘的试就行了呗!
jovinmavis 2010-06-09
  • 打赏
  • 举报
回复
看api
wei_wxx 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wei_wxx 的回复:]
File in = new File(input);
File out = new File(output);
// 如果没文件,创建一个
File parent = out.getParentFile();
if (!parent.exists())
parent.mkdirs();
out.createNewFile();
byte[] b = new byte[51200];
……
[/Quote]这个我能把一个图片放在令一个文件夹下。
但是我想得到这个图片的数据。然后通过这个数据做一些别的事情。比如。通过这些数据把图片放大或缩小。
wei_wxx 2010-06-09
  • 打赏
  • 举报
回复
File in = new File(input);
File out = new File(output);
// 如果没文件,创建一个
File parent = out.getParentFile();
if (!parent.exists())
parent.mkdirs();
out.createNewFile();
byte[] b = new byte[51200];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
in));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(out));
while (bis.available() > 0) {
bis.read(b);
bos.write(b);
}
bis.close();
bos.close();
wei_wxx 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dr_lou 的回复:]
File f = new File("d:/1.jpg");
InputStream is = new FileInputStream(f);
byte[] b = new byte[(int)f.length()];
is.read(b);
is.close();
return b;
[/Quote]
这个我写了。但是通过这个数据流把他放在另一个地方。他就读不出数据了。
水中影子 2010-06-09
  • 打赏
  • 举报
回复

[Quote=引用 2 楼 dr_lou 的回复:]
File f = new File("d:/1.jpg");
InputStream is = new FileInputStream(f);
byte[] b = new byte[(int)f.length()];
is.read(b);
is.close();
return b;
[/Quote]

焙焙龙 2010-06-09
  • 打赏
  • 举报
回复
任何文件都可以,而不仅仅是图片
创建一个InputStream就可以做到
  • 打赏
  • 举报
回复
只是一个io读取的问题而已。

--


多看看api,多Google一下就有了
dr_lou 2010-06-09
  • 打赏
  • 举报
回复
File f = new File("d:/1.jpg");
InputStream is = new FileInputStream(f);
byte[] b = new byte[(int)f.length()];
is.read(b);
is.close();
return b;
stl0 2010-06-09
  • 打赏
  • 举报
回复
我讨厌这种问问题的方式,不懈回答
wei_wxx 2010-06-09
  • 打赏
  • 举报
回复
不好意思。是我理解错了。我自己想的错了。
wei_wxx 2010-06-09
  • 打赏
  • 举报
回复
不过谢谢大家了。2楼的。谢谢了。虽然不是我要的效果。不过还是非常感谢。
wei_wxx 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 dr_lou 的回复:]
引用 8 楼 wei_wxx 的回复:
引用 7 楼 wei_wxx 的回复:
File in = new File(input);
File out = new File(output);
// 如果没文件,创建一个
File parent = out.getParentFile();
if (!parent.exists())
parent.mkdirs();
out.crea……
[/Quote]
本来我可以在form表单提交的时候获取一张图片的数据流。但是我现在需要审核。
所以必须先把一张图片存在令一个文件夹下。
这样我就没法办通过formFile自带的.getFileData()的方法获得数据流了。
如果有方法可以代替FormFile.getFileData()方法获取数据流也可以。
zwchunk 2010-06-09
  • 打赏
  • 举报
回复
up.
ronniegxq 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dr_lou 的回复:]

File f = new File("d:/1.jpg");
InputStream is = new FileInputStream(f);
byte[] b = new byte[(int)f.length()];
is.read(b);
is.close();
return b;
[/Quote]

顶之
dr_lou 2010-06-09
  • 打赏
  • 举报
回复
dr_lou 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wei_wxx 的回复:]
引用 7 楼 wei_wxx 的回复:
File in = new File(input);
File out = new File(output);
// 如果没文件,创建一个
File parent = out.getParentFile();
if (!parent.exists())
parent.mkdirs();
out.createNewFile();
byte[] ……
[/Quote]

如果要对图片大小等做文章,建议看看Image类的相关东西吧。

添加包: 
import com.sun.image.codec.jpeg.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.BufferedImageOp;
import java.awt.image.*;
添加类成员:
private InputStream inputStream;
private ByteArrayOutputStream bout;
private BufferedImage bufferedImage;


ImageIcon mi = new ImageIcon( "E:\\samples\\java\\SailiTest\\T18Big.gif ");
this.saveImage(mi.getImage(), "e:\\2.jpg ");//存成800*600的图像

//转换方法
public void saveImage(Image pImage,String ofileDirAndName)
{
BufferedImage bimg = null;
int w = pImage.getWidth(null);
int h = pImage.getHeight(null);
int [] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(pImage,0, 0, w, h,pixels, 0, w);
try {
pg.grabPixels();
}
catch(InterruptedException e) {
e.printStackTrace();
}
this.bout= new ByteArrayOutputStream();System.out.println( "3 ");
System.out.println( "图像宽高: " +w+ ": "+h );
BufferedImage bufferedImage= new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);

bufferedImage.setRGB(0,0,800,600,pixels,0,w);
float[] sharpKernel = { 0.0f, -1.0f, 0.0f,-1.0f, 5.0f, -1.0f, 0.0f, -1.0f, 0.0f };System.out.println( "5 ");
BufferedImageOp sharpen= new ConvolveOp( new Kernel(3, 3, sharpKernel), ConvolveOp.EDGE_NO_OP, null);
BufferedImage sharp= sharpen.filter( bufferedImage, null );
try
{
JPEGEncodeParam jpegParam= JPEGCodec.getDefaultJPEGEncodeParam( sharp );
jpegParam.setQuality( 1.0f, false );
JPEGImageEncoder jpeg= JPEGCodec.createJPEGEncoder( bout, jpegParam );
jpeg.encode( bufferedImage );
this.bout.writeTo(new FileOutputStream(ofileDirAndName));
}
catch(Exception ee)
{
System.err.println( "出错了 "+ee);
}
}


搜的前人写的代码,你看看。

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧