「java读取jpg」JAVA读取文件

博主:adminadmin 2023-01-17 23:45:09 355

今天给各位分享java读取jpg的知识,其中也会对JAVA读取文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java中 如何将存放在数据库中的pdf、doc、jpg等文件读出来(二进制形式存放在数据)

在数据库中存放这些个二进制文件的字段是BLOB,oracle和MysqL里面都是

java中读取 BLOB数据:

首先做查询,拿到查询结果ResultSet rs = XXXX (和普通数据查询一样)

然后:Blob blob = rs.getBlob("字段名"); 拿到你的Blob ,

得到文件的二进制流:InputStream binaryStream= blob.getBinaryStream();,

你的文件数据就在这个流当中,你想怎么用就怎么取,比如,读出来存到一个byte[]中,以便序列化传输,读出来构造成一个File直接存放到本地等等。

举个例子吧:从这个binaryStream中读取数据到byte[]的方法,

////////---------------------

/**

* 从binaryStream中读取数据到byte[]的方法

* @param in 即binaryStream

* @return

* @throws Exception

*/

public static byte[] readStreamToByteArray(InputStream in) throws Exception{

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len = -1;

while((len = in.read(buffer)) != -1) {

outputStream.write(buffer, 0, len);

}

outputStream.close();

in.close();

return outputStream.toByteArray();

}

//

用java实现读取一个jpg图片,根据4个坐标,把坐标内的图片保存为另一个jpg图片

package test.any;

import java.awt.Rectangle;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Iterator;

import javax.imageio.ImageIO;

import javax.imageio.ImageReadParam;

import javax.imageio.ImageReader;

import javax.imageio.stream.ImageInputStream;

public class CutPicture {

 private String srcpath;

 private String subpath;

 private String imageType;

 private int x;

 private int y;

 private int width;

 private int height;

 public CutPicture() {

 }

 

 public CutPicture(String srcpath, int x, int y, int width, int height) {

  this.srcpath = srcpath;

  this.x = x;

  this.y = y;

  this.width = width;

  this.height = height;

 }

 public int getHeight() {

  return height;

 }

 public void setHeight(int height) {

  this.height = height;

 }

 public String getSrcpath() {

  return srcpath;

 }

 public void setSrcpath(String srcpath) {

  this.srcpath = srcpath;

  if(srcpath != null) {

   this.imageType = srcpath.substring(srcpath.indexOf(".")+1, srcpath.length());

  }

 }

 public String getSubpath() {

  return subpath;

 }

 public void setSubpath(String subpath) {

  this.subpath = subpath;

 }

 public int getWidth() {

  return width;

 }

 public void setWidth(int width) {

  this.width = width;

 }

 public int getX() {

  return x;

 }

 public void setX(int x) {

  this.x = x;

 }

 public int getY() {

  return y;

 }

 public void setY(int y) {

  this.y = y;

 }

 

 public String getImageType() {

  return imageType;

 }

 public void setImageType(String imageType) {

  this.imageType = imageType;

 }

 

 public void cut() throws IOException {

  FileInputStream is = null;

  ImageInputStream iis = null;

  try {

   is = new FileInputStream(srcpath);

   this.setSrcpath(srcpath);

   IteratorImageReader it = ImageIO.getImageReadersByFormatName(this.imageType);

   ImageReader reader = it.next();

   iis = ImageIO.createImageInputStream(is);

   reader.setInput(iis, true);

   ImageReadParam param = reader.getDefaultReadParam();

   Rectangle rect = new Rectangle(x, y, width, height);

   param.setSourceRegion(rect);

   BufferedImage bi = reader.read(0, param);

   ImageIO.write(bi, this.imageType, new File(subpath));

  } finally {

   if (is != null)

    is.close();

   if (iis != null)

    iis.close();

  }

 } 

 

 public static void main(String[] args) {

  CutPicture o = new CutPicture("c:\\粗切编目层详细1.png", 0, 0, 100, 100);

  o.setSubpath("c:\\1.png");

  try {

   o.cut();

  } catch (IOException e) {

   e.printStackTrace();

  }

 }

}

请教如何用Java语言读取jpg图片,并显示

1、获取文件夹的路径 2、得到文件夹中的有图片的名称,可以存到数组或者集合中 3、你再到jsp页面做显示, 4、下面是获取路径和文件名的代码,前台显示的代码自己写 String path = 文件夹路径; String names = ""; try { File f = new File(path)

Java读取Jpg报错:Unsupported Image Type

意思是你指定的图片文件格式有错误。

以下是ImageIO兼容的图片格式。JPG也分很多种的。

bmp gif jpeg jpeg-lossless jpeg2000 png pnm raw tiff wbmp

jpg的分类多,如果麻烦,建议吧图片都改成bmp,这样系统也比较好认。

java读取jpg的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于JAVA读取文件、java读取jpg的信息别忘了在本站进行查找喔。