如何在java中读取图像字节?

时间:2016-01-29 07:30:57

标签: java

我正在尝试在jsp页面中显示图像,因为我正在使用波纹管代码。一切都很好,但显示了一些错误,我不知道如何解决这个问题,任何人都可以帮我解决这个问题。

id  user_id     friend_id

 134       1            2
  82       1            5
  48       1            4
  28       4            1
  10       6            1

错误

  <%@page import="java.awt.image.BufferedImage"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.io.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
InputStream inStream = null;
BufferedInputStream bis = null;
BufferedImage bImage=null;

inStream = new FileInputStream("/home/anand/Desktop/encrypt/firstBG.jpg");
bis = new BufferedInputStream(inStream);
int numByte = bis.available();
byte[] buf = new byte[numByte];
bis.read(buf, 2, 3);


 bImage = ImageIO.read(new ByteArrayInputStream(buf));//give the path of an image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bImage, "jpg", baos );
baos.flush();
byte[] imageInByteArray = baos.toByteArray();
baos.close();
String b64 = javax.xml.bind.DatatypeConverter.printBase64Binary(imageInByteArray);
%>

<div>
    <p>As of v6, Java SE provides JAXB</p>
    <img src="data:image/jpg;base64, <%=b64%>" alt="Visruth.jpg not found" />
</div>          
</body>
</html>

我知道问题是在BufferedImage中写入字节但我不知道如何解决这个问题。

0 个答案:

没有答案