码迷,mamicode.com
首页 > 其他好文 > 详细

ImgFormatUtils(图片拉伸)

时间:2018-08-02 14:53:42      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:raw   read   red   div   port   java   字节   输出流   utils   

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class ImgFormatUtils {


    public static void formatImage(String file) {
        //读取图片
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //字节流转图片对象
        Image bi = null;
        try {
            bi = ImageIO.read(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //构建图片流
        BufferedImage tag = new BufferedImage(400, 700, BufferedImage.TYPE_INT_RGB);
        //绘制改变尺寸后的图
        tag.getGraphics().drawImage(bi, 0, 0,400, 700, null);
        //输出流
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        try {
            encoder.encode(tag);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        try {
//            ImageIO.write(tag, "JPG",out);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        finally {

            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }




    public static byte[] formatImage2Bytes(String file) {
        //读取图片
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //字节流转图片对象
        Image bi = null;
        try {
            bi = ImageIO.read(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //构建图片流
        BufferedImage tag = new BufferedImage(400, 700, BufferedImage.TYPE_INT_RGB);
        //绘制改变尺寸后的图
        tag.getGraphics().drawImage(bi, 0, 0,400, 700, null);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(tag, "JPG",out);
            InputStream is = new ByteArrayInputStream(out.toByteArray());
            byte[] buff  = new byte[200];
            int rc  = 0;
            while (-1 != (rc  = is.read(buff , 0 , 200)) ){
                out.write(buff, 0, rc);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
            } catch(IOException e){}

        }
        return out.toByteArray();
    }


}

 

ImgFormatUtils(图片拉伸)

标签:raw   read   red   div   port   java   字节   输出流   utils   

原文地址:https://www.cnblogs.com/blakflash000/p/9406820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!