① java中如何把图片转换成二进制流
Java中将图片转为二进制流只需要使用FileImageInputStream取得图片文件,然后使用ByteArrayOutputStream 写入到二进制流中即可,下面是详细代码:
//图片到byte数组
publicbyte[]image2byte(Stringpath){
byte[]data=null;
FileImageInputStreaminput=null;
try{
input=newFileImageInputStream(newFile(path));
ByteArrayOutputStreamoutput=newByteArrayOutputStream();
byte[]buf=newbyte[1024];
intnumBytesRead=0;
while((numBytesRead=input.read(buf))!=-1){
output.write(buf,0,numBytesRead);
}
data=output.toByteArray();
output.close();
input.close();
}
catch(FileNotFoundExceptionex1){
ex1.printStackTrace();
}
catch(IOExceptionex1){
ex1.printStackTrace();
}
returndata;
}
另外,如果需要将byte[]存回图片或转为String,则:
//byte数组到图片
publicvoidbyte2image(byte[]data,Stringpath){
if(data.length<3||path.equals(""))return;
try{
=newFileImageOutputStream(newFile(path));
imageOutput.write(data,0,data.length);
imageOutput.close();
System.out.println("MakePicturesuccess,Pleasefindimagein"+path);
}catch(Exceptionex){
System.out.println("Exception:"+ex);
ex.printStackTrace();
}
}
//byte数组到16进制字符串
publicStringbyte2string(byte[]data){
if(data==null||data.length<=1)return"0x";
if(data.length>200000)return"0x";
StringBuffersb=newStringBuffer();
intbuf[]=newint[data.length];
//byte数组转化成十进制
for(intk=0;k<data.length;k++){
buf[k]=data[k]<0?(data[k]+256):(data[k]);
}
//十进制转化成十六进制
for(intk=0;k<buf.length;k++){
if(buf[k]<16)sb.append("0"+Integer.toHexString(buf[k]));
elsesb.append(Integer.toHexString(buf[k]));
}
return"0x"+sb.toString().toUpperCase();
}
② JAVA图片输出
等着拿分.......
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* @author alanwei
*
*/
public class Test {
public static BufferedImage createImage(int width, int height, String s) {
Font font = new Font("Serif", Font.BOLD, 10);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.RED);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(s, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(s, (int)x, (int)baseY);
return bi;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedImage image = createImage(100, 20, "123456789");
File file = new File("image.jpg");
if (!file.exists()) {
file.createNewFile();
}
if (image != null) {
ImageIO.write(image, "jpg", file);
}
}
}
③ JAVA如何直接在console控制台上用IO流输出图片急求!
解决方法:int len = fis.read(); read 方法加入参数bys,这样才能把fis的内容注入bys里面。
顺便说下,FileInputStream不能正确输出中文,因为这个是按字节输出的,每个中文站2个字节,会出现乱码。
④ 怎样用JAVA编写一个小应用程序,输出一张图片或自己的照片
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class text
{
JFrame jf;
JLabel jl;
JPanel jp1;
public static void main(String[] args)
{
text t=new text();
t.go();
}
void go()
{
final ImageIcon image1 = new ImageIcon("1.gif");
jf=new JFrame();
jl = new JLabel(image1);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300, 300);
jf.setLocation(400, 300);
jp1 = new JPanel();
jp1.add(jl);
jf.getContentPane().add(jp1);
jf.setVisible(true);
}
}
把你的找一张gif 格式的照片 .名字改成1.gif 放在你的项目根目录下
⑤ 请问 用java语句输出如图片图案应该怎么做
1、代码如下:
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World!");
//主循环
for(int i =10;i>0;i--){
//输出空格
for(int k=i;k>0;k--){System.out.print(" ");}
//输出数字
for(int j=i;j<=10;j++){
System.out.print(j+" ");
}System.out.println(" ");
}
}
}
2、效果如图
⑥ JAVA 输出图片代码
假如只在当前页面显示的话直接用 img src
在HTML(或者jsp)的body 里添加
<img src="test/1.jpg" />,
以上述例子说明,你现在所编辑的页面是index.jsp,那么test(文件夹)是跟此页面在同一目录中的,1.jpg是需要显示的图片,位置在test文件夹下。
总的意思是:文件夹与页面并列,图片在文件夹下。
⑦ java怎么在控制台输出一张jpg的图片
输出图片的base64编码
//imgFile是图片的路径
publicstaticvoidgetImageStr(StringimgFile){
InputStreaminputStream=null;
byte[]data=null;
try{
inputStream=newFileInputStream(imgFile);
data=newbyte[inputStream.available()];
inputStream.read(data);
inputStream.close();
}catch(IOExceptione){
e.printStackTrace();
}//加密
BASE64Encoderencoder=newBASE64Encoder();
System.out.println(encoder.encode(data));
}
⑧ java把图片转换成二进制流
public static void main(String[] args) throws Exception {
File file = new File("d:\L.jpg");//图片
FileInputStream fis = new FileInputStream(file);//把图片变成流
FileOutputStream fos = new FileOutputStream(new File("E:\L.jpg")); //把图片流写入E盘
byte[] read = new byte[1024]; //每次读取的字节 可以自己定义 256 512 1024 2048 等。。。
int len = 0;
while((len = fis.read(read))!= -1){ //读取变成流的图片
fos.write(read,0,len);//写入图片
}
fis.close();//关闭输入流
fos.close();//关闭输出流
}
⑨ java中输出图片的代码
final ImageView iv=(ImageView)findViewById(R.id.iv);
Button bt=(Button)findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View p1)
{
// TODO: Implement this method
if(iv.getDrawable()!=null)
iv.setImageResource(R.id.photo);
else iv.setImageResource(0);
}
});
⑩ Java编写时,怎么输出一张图片呀
(1)首先用ImageIO类读取这张图片
(2)如果要对这张图片修改,通过图片获取Graphics对象,再调用Graphics的方法来绘制、修改。
(3)再调用ImageIO的方法将图片输出到特定IO流即可。
具体代码实例可参考李刚的疯狂Java讲义的11.8节。