简单的java图片缩放代码
/**
* @param source: source image file
* @param output: output image file
* @param mode: 0 ratio,1 maxWidth,2 maxHeight,3 maxSide
* @param maxSide: maxWidth, maxHeight or maxSide, different by mode
* @param ratio: ratio if resized by ratio
*/
public static void resizeImage(File source, File output, int mode, Integer maxSide, Double ratio)
throws IOException {
// read image
Image sourceImage = ImageIO.read(source);
// resize image
BufferedImage outputImage = getResizeImage(sourceImage, mode, maxSide, ratio);
// wirite image to file
wirteImageToFile(outputImage, output);
}
/**
* @param source: source image
* @param mode: 0 ratio,1 maxWidth,2 maxHeight,3 maxSide
* @param maxSide: maxWidth, maxHeight or maxSide, different by mode
* @param ratio: ratio if resized by ratio
*/
public static BufferedImage getResizeImage(Image source, int mode, Integer maxSide, Double ratio) {
int srcImageWidth = source.getWidth(null);
int srcImageHeight = source.getHeight(null);
int width = 0, height = 0;
switch (mode) {
case 0:
// resize by ratio
width = (int) (srcImageWidth * ratio);
height = (int) (srcImageHeight * ratio);
break;
case 1:
// resize by max width
width = maxSide;
height = new BigDecimal(srcImageHeight).divide(new BigDecimal(srcImageWidth), 5, RoundingMode.HALF_EVEN)
.multiply(new BigDecimal(width)).intValue();
break;
case 2:
// resize by max height
height = maxSide;
width = new BigDecimal(srcImageWidth).divide(new BigDecimal(srcImageHeight), 5, RoundingMode.HALF_EVEN)
.multiply(new BigDecimal(height)).intValue();
break;
case 3:
// resize by max side(between width and height)
if (srcImageWidth > srcImageHeight) {
width = maxSide;
height = maxSide * srcImageHeight / srcImageWidth;
} else {
width = maxSide * srcImageWidth / srcImageHeight;
height = maxSide;
}
break;
}
BufferedImage t
相关文档:
Java把内存划分成两种:一种是栈内存,一种是堆内存。
在函数中定义的一些基本类型的变量和对象的引用变量都在函数的栈内存中分配。
当在一段代码块定义一个变量时,Java就在栈中为这个变量分配内存空间,当超过变量的作用域后,Java会自动释放掉为该变量 ......
keytool -genkey -dname "CN=demo, OU=softDept, O=company,
L=puddong,S=shanghai, C=cn" -alias demo -keyalg RSA -keysize 1024
-keystore demoKeystore -validity 3650 -storepass storePwd -keypass
demoPwd
生成保存公钥和私钥的密钥仓库,保存在demoKeystore文件中。这里storepass ......
import java.io.*;
public class FileReaderSample {
public static void main(String args[]) throws IOException
{
// 建立可容纳1024个字符的数组
char data[]=new char[1024];
// 建立对象fr
FileReader fr= ......
#dd_traces.pl (C) Marko Kivij?rvi 2006
# Dummy checks
die "Specify an input file!\n" if $ARGV[0] eq "";
die "File not found!\n" unless -e $ARGV[0];
die "Incorrect file extension for a C/C++ file!\n"
if ( $ARGV[0] !~ /(.*)\.(java)$/ );
# Constants
my $IMPORT_LOG_PACKAG = "\n ......
1. ObjectInputStream 与 ObjectOutputStream的读写对象可以以对应的顺序进行多个对象的读写。
2. Break Loop label。 在循环语句前加上一个label,如label1: 然后在循环体的语句中加入break label1,即可跳出该循环,无论其是内层循环
还是外层循环。 ......