JAVA切图 等比例缩放

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.MemoryCacheImageInputStream;


/**
 * 图片工具类
 * 
 * @author Administrator
 * 
 */
public final class ImageUtils {


public static final String PNG = "png";
public static final String JPG = "jpg";
public static final String BMP = "bmp";
public static final String GIF = "gif";


public static void main(String[] args) {
zoom("D:\\1.jpg", "D:\\2.jpg", 365, "jpg");
}


/**
* 缩放图片

* @param picFrom
*            源图片地址
* @param picTo
*            目标图片地址
* @param size
*            目标图片宽度
* @param formart
*            目标图片格式
* @return
*/
public static String zoom(String picFrom, String picTo, int size,
String formart) {
formart = JPG;
try {
byte[] k1 = readFromFile(picFrom);
BufferedImage img = getScaleImage(k1, formart.toUpperCase(), size);
RenderedImage rendImage = getScaleImage(k1, formart.toUpperCase(),
size);
// 以下是将图形保存为标准图片格式
ImageIO.write(rendImage, formart.toUpperCase(), new File(picTo));
} catch (IOException e) {


}
return picTo;
}


public static byte[] readFromFile(String path) throws IOException {
InputStream is = new FileInputStream(new File(path));
byte[] buf = new byte[is.available()];
is.read(buf);
is.close();
return buf;
}


/**
* 构建一个image对象

* @param img
* @return
* @throws IOException
*/
public static ImageInfo getImageInfo(byte[] img) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(img);
MemoryCacheImageInputStream is = new MemoryCacheImageInputStream(bais);
Iterator<ImageReader> it = ImageIO.getImageReaders(is);
ImageReader r = null;
while (it.hasNext()) {
r = it.next();
break;
}
if (r == null) {
return null;
}
ImageInfo i = new ImageInfo();
i.setType(r.getFormatName().toLowerCase());
int index = r.getMinIndex();
/**
* 对于ImageReader的线程安全是不确定的
*/
synchronized (r) {
r.setInput(is);
i.setHeight(r.getHeight(index));
i.setWidth(r.getWidth(index));
}
return i;
}


public static BufferedImage getImage(byte[] img) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(img);
BufferedImage src = ImageIO.read(bais);
return src;
}


/**
* 等比例缩放

* @param img
* @param width
* @return
* @throws IOException
*/
public static BufferedImage getScaleImage(byte[] img, String type, int width)
throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(img);
BufferedImage src = ImageIO.read(bais);
int w = src.getWidth();
int h = src.getHeight();
int height = (int) (((float) width / w) * h);
Image im = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage bi = new BufferedImage(width, height, src.getType());
bi.getGraphics().drawImage(im, 0, 0, null);
return bi;
}


public static byte[] getScaleImageBytes(byte[] img, String type, int width)
throws IOException {
BufferedImage bi = getScaleImage(img, type, width);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bi, type, out);
return out.toByteArray();
}


/**
* 获取文件类型,没找到返回null,这方法太高效了,可能不准确, 这个是我看的网上的,有bug不准确

* @param byte1
* @return
*/
public static String fastParseFileType(byte[] byte1) {
if ((byte1[0] == 71) && (byte1[1] == 73) && (byte1[2] == 70)
&& (byte1[3] == 56) && ((byte1[4] == 55) || (byte1[4] == 57))
&& (byte1[5] == 97)) {
return GIF;
}
if ((byte1[6] == 74) && (byte1[7] == 70) && (byte1[8] == 73)
&& (byte1[9] == 70)) {
return JPG;
}
if ((byte1[0] == 66) && (byte1[1] == 77)) {
return BMP;
}
if ((byte1[1] == 80) && (byte1[2] == 78) && (byte1[3] == 71)) {
return PNG;
}
return null;
}


public static class ImageInfo {
private String type;
private int width;
private int height;


public String getType() {
return type;
}


public void setType(String type) {
this.type = type;
}


public int getWidth() {
return width;
}


public void setWidth(int width) {
this.width = width;
}


public int getHeight() {
return height;
}


public void setHeight(int height) {
this.height = height;
}
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linyu19872008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值