package com.pic;import java.io.*;
import java.awt.*;
import java.awt.image.*;import java.awt.Graphics;
import java.awt.color.ColorSpace;import javax.imageio.ImageIO;public class ImageCut { /**
 * 
 * 缩放图像
 * 
 * @param srcImageFile源图像文件地址
 * 
 * @param result缩放后的图像地址
 * 
 * @param scale缩放比例
 * 
 * @param flag缩放选择
 *            :true 放大; false 缩小;
 */ public static void scale(String srcImageFile, String result, int scale, boolean flag) { try { BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件 int width = src.getWidth(); // 得到源图宽 int height = src.getHeight(); // 得到源图长 if (flag) {// 放大 width = width * scale; height = height * scale; } else {// 缩小 width = width / scale; height = height / scale; } Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(image, 0, 0, null); // 绘制缩小后的图 g.dispose(); ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流 } catch (IOException e) { e.printStackTrace(); } } /**
 * 
 * 图像切割
 * 
 * @param srcImageFile源图像地址
 * 
 * @param descDir切片目标文件夹
 * 
 * @param destWidth目标切片宽度
 * 
 * @param destHeight目标切片高度
 */ public static void cut(String srcImageFile, String descDir, int destWidth, int destHeight) { try { Image img; ImageFilter cropFilter; // 读取源图像 BufferedImage bi = ImageIO.read(new File(srcImageFile)); int srcWidth = bi.getHeight(); // 源图宽度 int srcHeight = bi.getWidth(); // 源图高度 if (srcWidth > destWidth && srcHeight > destHeight) { Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); destWidth = 200; // 切片宽度 destHeight = 150; // 切片高度 int cols = 0; // 切片横向数量 int rows = 0; // 切片纵向数量 // 计算切片的横向和纵向数量 if (srcWidth % destWidth == 0) { cols = srcWidth / destWidth; } else { cols = (int) Math.floor(srcWidth / destWidth) + 1; } if (srcHeight % destHeight == 0) { rows = srcHeight / destHeight; } else { rows = (int) Math.floor(srcHeight / destHeight) + 1; } // 循环建立切片 // 改进的想法:是否可用多线程加快切割速度 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { // 四个参数分别为图像起点坐标和宽高 // 即: CropImageFilter(int x,int y,int width,int height) cropFilter = new CropImageFilter(j * 200, i * 150, destWidth, destHeight); img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(img, 0, 0, null); // 绘制缩小后的图 g.dispose(); // 输出为文件 ImageIO.write(tag, "JPEG", new File(descDir + "pic_" + i + "_" + j + ".jpg")); } } } } catch (Exception e) { e.printStackTrace(); } } // 图像类型转换GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X) public static void convert(String source, String result) { try { File f = new File(source); f.canRead(); f.canWrite(); BufferedImage src = ImageIO.read(f); ImageIO.write(src, "JPG", new File(result)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 彩色转为黑白 public static void gray(String source, String result) { try { BufferedImage src = ImageIO.read(new File(source)); ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); ColorConvertOp op = new ColorConvertOp(cs, null); src = op.filter(src, null); ImageIO.write(src, "JPEG", new File(result)); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { cut("f:/1.jpg", "f:/1/", 20, 20);
System.out.println("切割完成");
}}切割类package com.pic;import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;import javax.imageio.ImageIO;public class ImagePluser {
/**
 * 功能:取出文件夹imageFilePath中所有的图片,拼接成一张图片,保存至路径finalPath + "\\" + finalName
 * 
 * @author Administrator
 * @param List
 *            <File> fileList,String finalPath, String finalNam
 * @return String 
 */
public static String plus(String imageFilePath, String finalPath,
String finalName) {
String finalPahtName = finalPath + "\\" + finalName;
File file = new File(imageFilePath);
File[] fileList = file.listFiles();
try {
int widthNew = 0;
int heightNew = 0;
List<BufferedImage> images = new ArrayList<BufferedImage>();
List<Integer> widths = new ArrayList<Integer>();
List<Integer> heights = new ArrayList<Integer>();
// 读图 计算宽高 read pictures and calculate the width and height of the
// final picture
for (int i = 0; i < fileList.length; i++) {
BufferedImage image = ImageIO.read(fileList[i]);
images.add(image);
widths.add(image.getWidth());
heights.add(image.getHeight());
if (widthNew < image.getWidth())
widthNew = image.getWidth();
heightNew += image.getHeight();
}
// 创建新图 写像素 creat a new picture and insert into all of the px
BufferedImage ImageNew = new BufferedImage(widthNew, heightNew,
BufferedImage.TYPE_INT_RGB);
// 设置背景 set the background color
Graphics g = ImageNew.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, widthNew, heightNew);
int heightTemp = 0;
for (int i = 0; i < images.size(); i++) {
// 绘制图片 draw the picture
ImageNew.setRGB(
(widthNew - widths.get(i)) / 2,
heightTemp,
widths.get(i),
heights.get(i),
images.get(i).getRGB(0, 0, widths.get(i),
heights.get(i),
new int[widths.get(i) * heights.get(i)], 0,
widths.get(i)), 0, widths.get(i));
heightTemp += heights.get(i);
}
// 保存新图 save the new picture
File outFile = new File(finalPahtName);
ImageIO.write(ImageNew, "jpg", outFile);
} catch (Exception e) {
e.printStackTrace();
finalPahtName = "";
}
return finalPahtName;
} public static void main(String[] args) {
plus("f:/1/", "f:", "t.jpg");
System.out.println("拼接完成");
}}
拼接类
求怎么得到原图大小widths.add(image.getWidth());
heights.add(image.getHeight());这里得到的是第一张图片 大小

解决方案 »

  1.   

     BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
     
                int width = src.getWidth(); // 得到源图宽
     
                int height = src.getHeight(); // 得到源图长这不是文件长宽吗?
      

  2.   

     BufferedImage src = ImageIO.read(new File(srcImageFile)); 红色的传入你拼接的图片不行吗?一样的道理啊
      

  3.   

    修改你的default.properties,看看里面是什么,改为7
      

  4.   

    不好意思。。点引用。点错了。。
    我上面是一个集合。因为拼接是多张图片
    File[] fileList = file.listFiles();
     BufferedImage image = ImageIO.read(fileList[i]);
      

  5.   

    循环这个fileList[i],获取每张高宽,然后加就行了。