简单的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
相关文档:
public static void main(String args[]){
Date calr1=new Date(2010, 3,1);
Date calr2=new Date(2010,3,31);
Long n=calr2.getTime()-calr1.getTime();
int a=(int)(n/ ......
处理乱码问题
1、规范数据库和页面所有编码统一为一种例如utf-8
2、hibernate连接数据库设置连接编码
<session-factory>
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/leaveword?useUnicode=true&characterEncoding=utf-8
& ......
最近在学习java的时候,对于java函数的传递了解了一下。java的数据类型大的分为两种,一种是基本数据类型,另一种是非基本数据类型,(类数据类型)。在参数传递时候,基本的数据类型传递的是对实参数的一份拷贝,而非基本数据类型则是实例的引用的一份拷贝。我的理解是类的实例,即对象存放的内容就是引用,实际是地址。
......
这是一个简单的读取文件的代码,并试着读取一个log文件,再输出。
import java.io.*;
public class FileToString {
public static String readFile(String fileName) {
String output = "";
File file = new File(fileName);
if(file.exists()){
......