zoukankan      html  css  js  c++  java
  • java 实现 图片与byte 数组互相转换

    package webgate;
     
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
     
    //图片文件,与 byte[] 互转
    public class TestFile {
     
    	static byte[] bytes;
     
    	public static void main(String[] args) throws Exception {
    		File img = new File("W:\img\04.jpg");
    		fileToByte(img);
    		ByteToFile(bytes);
    	}
     
    	public static void fileToByte(File img) throws Exception {
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		try {
    			BufferedImage bi;
    			bi = ImageIO.read(img);
    			ImageIO.write(bi, "jpg", baos);
    			bytes = baos.toByteArray();
    			System.err.println(bytes.length);
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			baos.close();
    		}
    	}
    	
    	static void ByteToFile(byte[] bytes)throws Exception{ 
    		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);   
            BufferedImage bi1 =ImageIO.read(bais); 
            try {   
                File w2 = new File("W:\img\00000000003.jpg");//可以是jpg,png,gif格式   
                ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片,此处不需改动   
            } catch (IOException e) {   
                e.printStackTrace();   
            }finally{
            	bais.close();
            }
        }  
     
    }
    

      

  • 相关阅读:
    三数之和
    罗马数字与整数
    Oracle 开启或关闭归档
    Oracle RMAN scripts to delete archivelog
    Oracle check TBS usage
    Oracle kill locked sessions
    场景9 深入RAC运行原理
    场景7 Data Guard
    场景4 Data Warehouse Management 数据仓库
    场景5 Performance Management
  • 原文地址:https://www.cnblogs.com/fanblogs/p/11269824.html
Copyright © 2011-2022 走看看