public static void toJPG(String source, String dest, int quality) throws JimiException {
if (dest == null || dest.trim().equals(""))
dest = source;
if (!dest.toLowerCase().trim().endsWith("jpg")) {
dest += ".jpg";
System.out.println("Overriding to JPG, output file: " + dest);
}
if (quality < 0 || quality > 100 || (quality + "") == null
|| (quality + "").equals("")) {
System.out.println("quality must between ’0’ and ’100’");
System.out.println("set to DEFAULT value:’75’");
quality = 100;
}
try {
JPGOptions options = new JPGOptions();
options.setQuality(quality);
ImageProducer image = Jimi.getImageProducer(source);
JimiWriter writer = Jimi.createJimiWriter(dest);
writer.setSource(image);
// 加入属性设置,非必要
// /*
writer.setOptions(options);
// */
writer.putImage(dest);
OutputStream os = new ByteArrayOutputStream();
writer.putImage(os);
} catch (JimiException je) {
throw je;
}
}
public static void toJPG(InputStream input, String dest, int quality) throws JimiException {
/*if (dest == null || dest.trim().equals(""))
dest = source;*/
if (!dest.toLowerCase().trim().endsWith("jpg")) {
dest += ".jpg";
System.out.println("Overriding to JPG, output file: " + dest);
}
if (quality < 0 || quality > 100 || (quality + "") == null
|| (quality + "").equals("")) {
System.out.println("quality must between ’0’ and ’100’");
System.out.println("set to DEFAULT value:’75’");
quality = 100;
}
try {
JPGOptions options = new JPGOptions();
options.setQuality(quality);
//ImageProducer image = Jimi.getImageProducer(source);
ImageProducer image = Jimi.getImageProducer(input);
JimiWriter writer = Jimi.createJimiWriter(dest);
writer.setSource(image);
// 加入属性设置,非必要
// /*
writer.setOptions(options);
// */
writer.putImage(dest);
} catch (JimiException je) {
throw je;
}
}
/**
*
* 线性改变图片尺寸
*
* @param source
*
* 源文件完整路径
*
* @param desc
*
* 目标文件完整路径
*
* @param descWidth
*
* 图片期望宽度
*
* @param descHeight
*
* 图片期望高度
* @throws Exception
*
*/
public void changeDimension(String source, String desc, int descWidth,
int descHeight)
throws Exception {
String temp = desc;
File _file = null;
if (desc == null || desc.trim().equals(""))
desc = source;
if (!desc.toLowerCase().trim().endsWith("jpg")) {
temp = desc.trim() + ".jpg";
}
this.toJPG(source, temp, 100);
_file = new File(temp); // 读入文件
Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象
double wideth = (double) src.getWidth(null); // 得到源图宽
double height = (double) src.getHeight(null); // 得到源图长
int iWideth = descWidth;
int iHeight = descHeight;
BufferedImage tag = new BufferedImage(iWideth, iHeight,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图
if (!temp.trim().equals(desc))
_file.deleteOnExit();
if (desc.toLowerCase().trim().endsWith("jpg")) {
JimiWriter writer = Jimi.createJimiWriter(desc);
writer.setSource(tag);
writer.putImage(desc);
} else {
throw new Exception("不支持此格式的图片尺寸的转换");
}
}
/**
*
* 线性改变图片尺寸(可同时改变图片格式)
*
*
*
* @param source
*
* 源文件完整路径
*
* @param desc
*
* 目标文件完整路径
*
* @param ins
*
* 放大/缩小比率
* @throws Exception
*
*/
public void changeDimension(String source, String desc, double percent)
throws Exception {
String temp = desc;
File _file = null;
if (desc == null || desc.trim().equals(""))
desc = source;
if (!desc.toLowerCase().trim().endsWith("jpg")) {
temp = desc.trim() + ".jpg";
}
this.toJPG(source, temp, 100);
_file = new File(temp); // 读入文件
Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象
double wideth = (double) src.getWidth(null); // 得到源图宽
double height = (double) src.getHeight(null); // 得到源图长
int iWideth = (int) (wideth * percent);
int iHeight = (int) (height * percent);
BufferedImage tag = new BufferedImage(iWideth, iHeight,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图
if (!temp.trim().equals(desc))
_file.deleteOnExit();
if (desc.toLowerCase().trim().endsWith("jpg")) {
JimiWriter writer = Jimi.createJimiWriter(desc);
writer.setSource(tag);
writer.putImage(desc);
} else {
throw new Exception("不支持此格式的图片尺寸的转换");
}
}
public byte [] toJPG(String source) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
File fin = new File(source);
BufferedImage input = ImageIO.read(fin);
ImageIO.write(input, "jpg", os);
byte [] bytes = os.toByteArray();
return bytes;
}
public byte[] toJPG(byte[] tempbytes) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream in = new ByteArrayInputStream(tempbytes);
BufferedImage input = ImageIO.read(in);
ImageIO.write(input, "jpg", os);
byte [] bytes = os.toByteArray();
return bytes;
}
public byte [] changeDimension(InputStream is,int expectWidth,int expectHeight) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
BufferedImage input = ImageIO.read(is);
BufferedImage scaledBI = new BufferedImage(expectWidth, expectHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = scaledBI.createGraphics();
g.setComposite(AlphaComposite.Src);
g.drawImage(input, 0, 0, expectWidth, expectHeight, null);
g.dispose();
ImageIO.write(scaledBI, "jpg", os);
byte [] descBytes = os.toByteArray();
return descBytes;
}
public static boolean isJPG(byte[] bytes) {
if (String.valueOf(bytes[0]).equals("-1") && String.valueOf(bytes[1]).equals("-40") && String.valueOf(bytes[2]).equals("-1") /*&& String.valueOf(bytes[3]).equals("-32")*/)
return true;
else
return false;
}
public static boolean isPNG(byte[] bytes) {
if(String.valueOf(bytes[0]).equals("-119") && String.valueOf(bytes[1]).equals("80") && String.valueOf(bytes[2]).equals("78") && String.valueOf(bytes[3]).equals("71"))
return true;
else
return false;
}
public static boolean isGIF(byte[] bytes) {
if (String.valueOf(bytes[0]).equals("71") && String.valueOf(bytes[1]).equals("73") && String.valueOf(bytes[2]).equals("70"))
return true;
else
return false;
}
public static boolean isBMP(byte[] bytes) {
if (String.valueOf(bytes[0]).equals("66") && String.valueOf(bytes[1]).equals("77"))
return true;
else
return false;
}
public static byte[] getBytes(String filePath) throws Exception {
File file = new File(filePath);
InputStream input = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte [] bytes = new byte [4096];
int length = input.read(bytes);
while(length != -1) {
baos.write(bytes,0,length);
length = input.read(bytes);
}
byte [] tempByte = baos.toByteArray();
baos.close();
input.close();
return tempByte;
}
public double getPicLength(InputStream input) throws IOException {
long lBytes = input.available();
return ((double)lBytes/1024)/1024;
}