当前位置:编程学习 > JAVA >>

哪位大神来帮忙解决下,图片切割 拼接的时候,不知道怎么得到原图长,宽

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());
这里得到的是第一张图片 大小 --------------------编程问答-------------------- 切割的时候,每个小片的大小应该能获取吧? --------------------编程问答-------------------- 是我自己设置的,20X20 。 --------------------编程问答-------------------- 关键我不知道生成了几个。 就一个i和j判断的。循环。。 代码在上面。。 --------------------编程问答-------------------- 你这原图指的是哪个原图?
最开始的那个图还是切割后还未组装的还是? --------------------编程问答-------------------- 你肯定设置一个比例去切割,而不是切割后再设置。 --------------------编程问答--------------------
引用 4 楼 AA5279AA 的回复:
你这原图指的是哪个原图?
最开始的那个图还是切割后还未组装的还是?

就是没切割的图片。
我要知道那个长,宽,但是 要从切割后的小图片去判断吧。。。 --------------------编程问答--------------------
引用 5 楼 fangmingshijie 的回复:
你肯定设置一个比例去切割,而不是切割后再设置。
  是设置了比例啊。20X20、
但是我不晓得与原图大小啊。。。。
我要切割之后能拼接成原图。
现在的代码是  
从上往下生成的。 --------------------编程问答--------------------  BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
 
            int width = src.getWidth(); // 得到源图宽
 
            int height = src.getHeight(); // 得到源图长

这不是文件长宽吗? --------------------编程问答--------------------
引用 8 楼 fangmingshijie 的回复:
 BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
 
            int width = src.getWidth(); // 得到源图宽
 
            int height = src.getHeight(); // 得到源图长

这不是文件长宽吗?

这是 切割图片的。
我要的是 拼接图片,通过 生成的文件名,来生成整图的长,宽。
--------------------编程问答--------------------  BufferedImage src = ImageIO.read(new File(srcImageFile)); 红色的传入你拼接的图片不行吗?一样的道理啊 --------------------编程问答-------------------- 修改你的default.properties,看看里面是什么,改为7 --------------------编程问答--------------------
引用 11 楼 fangmingshijie 的回复:
修改你的default.properties,看看里面是什么,改为7
回复错误,sorry --------------------编程问答--------------------
引用 10 楼 fangmingshijie 的回复:
 BufferedImage src = ImageIO.read(new File(srcImageFile)); 红色的传入你拼接的图片不行吗?一样的道理啊

。。。不好意思。。点引用。点错了。。
我上面是一个集合。因为拼接是多张图片。。。
File[] fileList = file.listFiles();
 BufferedImage image = ImageIO.read(fileList[i]); --------------------编程问答-------------------- 循环这个fileList[i],获取每张高宽,然后加就行了。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,