Java 图像处理需求 请各位高手不吝赐教:我现在有一个这样的需求:就是把16张jpg格式的图片,拼凑在一张大图片上,并且要形成4*4的排列,请高手们指点编程思路 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用JMagick吧 ~~~ 用DIV层控制,很容易实现,具体看我的博客http://chengyu2099.javaeye.com/admin/blogs/572687 我是写后台程序的,怎么会用到div层呢。。?谢谢你的答案 这里给你一个2*2的。4*4的自己仿照写即可。 File f1=new File("D:\\test\\image\\1.jpg"); File f2=new File("D:\\test\\image\\2.jpg"); File f3=new File("D:\\test\\image\\3.jpg"); File f4=new File("D:\\test\\image\\4.jpg"); BufferedImage bi1=ImageIO.read(f1); BufferedImage bi2=ImageIO.read(f2); BufferedImage bi3=ImageIO.read(f3); BufferedImage bi4=ImageIO.read(f4); int[]image1RGB=new int[bi1.getHeight()*bi1.getWidth()]; image1RGB=bi1.getRGB(0 , 0, bi1.getWidth(), bi1.getHeight(), image1RGB, 0, bi1.getWidth()); int[]image2RGB=new int[bi2.getHeight()*bi2.getWidth()]; image2RGB=bi2.getRGB(0 , 0, bi2.getWidth(), bi2.getHeight(), image2RGB, 0, bi2.getWidth()); int[]image3RGB=new int[bi3.getHeight()*bi3.getWidth()]; image3RGB=bi3.getRGB(0 , 0, bi3.getWidth(), bi3.getHeight(), image3RGB, 0, bi3.getWidth()); int[]image4RGB=new int[bi4.getHeight()*bi4.getWidth()]; image4RGB=bi4.getRGB(0 , 0, bi4.getWidth(), bi4.getHeight(), image4RGB, 0, bi4.getWidth()); //四张图片大小相等的时候下面即可,不等定义的时候要注意长和宽 BufferedImage ImageNew = new BufferedImage(bi1.getWidth()+bi2.getWidth(),bi1.getHeight()+bi3.getHeight(),BufferedImage.TYPE_INT_RGB); ImageNew.setRGB(0,0, bi1.getWidth(), bi1.getHeight(), image1RGB, 0, bi1.getWidth()); ImageNew.setRGB(bi1.getWidth(),0, bi2.getWidth(), bi2.getHeight(), image2RGB, 0, bi2.getWidth()); ImageNew.setRGB(0,bi1.getHeight(), bi3.getWidth(), bi3.getHeight(), image3RGB, 0, bi3.getWidth()); ImageNew.setRGB(bi1.getWidth(),bi1.getHeight(), bi4.getWidth(), bi4.getHeight(), image4RGB, 0, bi4.getWidth()); ImageIO.write(ImageNew, "jpg", new File("D:\\test\\image\\new.jpg")); 我看到了希望的曙光了,主要是我以前从来没有用java来处理过图像。今天刚分配给我的任务,真是感谢了 你可以这样,使用GirdLayout布局窗体JFrame,分成4行4列,在每个小块上添加JPanel,把你的图片一次放在JPanel上,不知道是否满足楼主的需求? 我倒是有些处理BMP的代码(提取点阵,图像操作等) 看看java 2d(这个事自己画图),或者用swing的分布,把四张图画在frame上 恩,是这样的,我一直就没有学swing这方面的东西,所以做起来有点困难,这几天才接手这个事情啊,我得找资料,把这个东西做出来,谢谢你的答案 用了一上午时间终于做出来了 /** */ /** * 缩放图像 * * @param file 源图像文件地址 * @param result 缩放后的图像地址 * @param scale 缩放比例 * @param flag 缩放选择:true 放大; false 缩小; */ public static void reduceImg(File imgsrc, int widthdist, int heightdist) { try { if (!imgsrc.exists()) { return; } Image src = javax.imageio.ImageIO.read(imgsrc); BufferedImage tag = new BufferedImage(widthdist, heightdist, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null); FileOutputStream out = new FileOutputStream(imgsrc); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } catch (IOException ex) { ex.printStackTrace(); } } public void testMethodImage() { File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\images\\img"); File[] list = file.listFiles(); BufferedImage ImageNew = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB); int startX = 0; int startY = 0; int flag = 0; for (int i = 0; i < 16; i++) { BufferedImage buffer = null; try { //System.out.println(list[i]);// MmsCoder.resizeImage(new FileInputStream(list[i]), new FileOutputStream(list[i]), 50, 50); //MmsCoder.resizeImage(list[i],list[i] ,150, 150); reduceImg(list[i], 200, 200); drawImage(list[i]) ; buffer = ImageIO.read(list[i]); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } int[] imageRGB = new int[buffer.getHeight() * buffer.getWidth()]; imageRGB = buffer.getRGB(0, 0, buffer.getWidth(), buffer.getHeight(), imageRGB, 0, buffer.getWidth()); if (flag % 2 == 0) { ImageNew.setRGB(startX, startY, buffer.getWidth(), buffer.getHeight(), imageRGB, 0, buffer.getWidth()); startX += 200; if (startX == 800) { startX = 0; startY += 200; flag += 1; } } if (flag % 2 != 0) { ImageNew.setRGB(startX, startY, buffer.getWidth(), buffer.getHeight(), imageRGB, 0, buffer.getWidth()); startX += 200; if (startX == 800) { startX = 0; startY += 200; flag += 1; } } if(flag==4){ break; } } try { ImageIO.write(ImageNew, "jpg", new File("C:\\Documents and Settings\\Administrator\\桌面\\images\\img\\all.jpg")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * * 在图片文件上加上图片的名字 * @param file */ public void drawImage(File file) { BufferedImage image = null; try { image = ImageIO.read(file); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Graphics g = image.getGraphics(); g.setColor(Color.blue); g.setFont(new Font("微软雅黑",Font.BOLD,40)) ; g.drawString(file.getName().substring(0, 5), image.getWidth()-115, image.getHeight()); try { ImageIO.write(image, "JPEG", file); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 大哥们帮我看看mysql中文乱码问题 我点JFame窗体的自带退出关闭(那红叉),界面是退出。但是进程还在呢!为什么? 在纯JDK系统里,怎么使用全局变量 很费解的题目大家帮着看看 如何在JTable中的以行为单位画虚线 [java 问题] 看看为什么运行错误 关于exe4j的问题,高手请帮忙啊 请大家帮助:用JAVA连接数据库的方式 如何得到一个long类型变量的显示长度 final String 成员变量还能通过构造器赋值? SAX解析带有.xsd Schema(是W3C的,不是XDR)的XML文档? wait()和notify()简单问题
http://chengyu2099.javaeye.com/admin/blogs/572687
我是写后台程序的,怎么会用到div层呢。。?谢谢你的答案
File f1=new File("D:\\test\\image\\1.jpg");
File f2=new File("D:\\test\\image\\2.jpg");
File f3=new File("D:\\test\\image\\3.jpg");
File f4=new File("D:\\test\\image\\4.jpg");
BufferedImage bi1=ImageIO.read(f1);
BufferedImage bi2=ImageIO.read(f2);
BufferedImage bi3=ImageIO.read(f3);
BufferedImage bi4=ImageIO.read(f4);
int[]image1RGB=new int[bi1.getHeight()*bi1.getWidth()];
image1RGB=bi1.getRGB(0 , 0, bi1.getWidth(), bi1.getHeight(), image1RGB, 0, bi1.getWidth());
int[]image2RGB=new int[bi2.getHeight()*bi2.getWidth()];
image2RGB=bi2.getRGB(0 , 0, bi2.getWidth(), bi2.getHeight(), image2RGB, 0, bi2.getWidth());
int[]image3RGB=new int[bi3.getHeight()*bi3.getWidth()];
image3RGB=bi3.getRGB(0 , 0, bi3.getWidth(), bi3.getHeight(), image3RGB, 0, bi3.getWidth());
int[]image4RGB=new int[bi4.getHeight()*bi4.getWidth()];
image4RGB=bi4.getRGB(0 , 0, bi4.getWidth(), bi4.getHeight(), image4RGB, 0, bi4.getWidth());
//四张图片大小相等的时候下面即可,不等定义的时候要注意长和宽
BufferedImage ImageNew = new BufferedImage(bi1.getWidth()+bi2.getWidth(),bi1.getHeight()+bi3.getHeight(),BufferedImage.TYPE_INT_RGB);
ImageNew.setRGB(0,0, bi1.getWidth(), bi1.getHeight(), image1RGB, 0, bi1.getWidth());
ImageNew.setRGB(bi1.getWidth(),0, bi2.getWidth(), bi2.getHeight(), image2RGB, 0, bi2.getWidth());
ImageNew.setRGB(0,bi1.getHeight(), bi3.getWidth(), bi3.getHeight(), image3RGB, 0, bi3.getWidth());
ImageNew.setRGB(bi1.getWidth(),bi1.getHeight(), bi4.getWidth(), bi4.getHeight(), image4RGB, 0, bi4.getWidth());
ImageIO.write(ImageNew, "jpg", new File("D:\\test\\image\\new.jpg"));
我看到了希望的曙光了,主要是我以前从来没有用java来处理过图像。今天刚分配给我的任务,真是感谢了
恩,是这样的,我一直就没有学swing这方面的东西,所以做起来有点困难,这几天才接手这个事情啊,我得找资料,把这个东西做出来,谢谢你的答案
/** */
/**
* 缩放图像
*
* @param file 源图像文件地址
* @param result 缩放后的图像地址
* @param scale 缩放比例
* @param flag 缩放选择:true 放大; false 缩小;
*/
public static void reduceImg(File imgsrc, int widthdist, int heightdist)
{
try
{
if (!imgsrc.exists())
{
return;
}
Image src = javax.imageio.ImageIO.read(imgsrc); BufferedImage tag = new BufferedImage(widthdist, heightdist, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null); FileOutputStream out = new FileOutputStream(imgsrc);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close(); }
catch (IOException ex)
{
ex.printStackTrace();
}
}
public void testMethodImage()
{
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\images\\img");
File[] list = file.listFiles();
BufferedImage ImageNew = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
int startX = 0;
int startY = 0;
int flag = 0;
for (int i = 0; i < 16; i++)
{ BufferedImage buffer = null;
try
{
//System.out.println(list[i]);
// MmsCoder.resizeImage(new FileInputStream(list[i]), new FileOutputStream(list[i]), 50, 50);
//MmsCoder.resizeImage(list[i],list[i] ,150, 150);
reduceImg(list[i], 200, 200);
drawImage(list[i]) ;
buffer = ImageIO.read(list[i]);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} int[] imageRGB = new int[buffer.getHeight() * buffer.getWidth()]; imageRGB = buffer.getRGB(0, 0, buffer.getWidth(), buffer.getHeight(), imageRGB, 0, buffer.getWidth()); if (flag % 2 == 0)
{
ImageNew.setRGB(startX, startY, buffer.getWidth(), buffer.getHeight(), imageRGB, 0, buffer.getWidth());
startX += 200;
if (startX == 800)
{
startX = 0;
startY += 200;
flag += 1;
} }
if (flag % 2 != 0)
{
ImageNew.setRGB(startX, startY, buffer.getWidth(), buffer.getHeight(), imageRGB, 0, buffer.getWidth());
startX += 200;
if (startX == 800)
{
startX = 0;
startY += 200;
flag += 1;
}
}
if(flag==4){
break;
}
}
try
{
ImageIO.write(ImageNew, "jpg", new File("C:\\Documents and Settings\\Administrator\\桌面\\images\\img\\all.jpg"));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} }
/**
*
* 在图片文件上加上图片的名字
* @param file
*/
public void drawImage(File file)
{
BufferedImage image = null;
try
{
image = ImageIO.read(file);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Graphics g = image.getGraphics();
g.setColor(Color.blue);
g.setFont(new Font("微软雅黑",Font.BOLD,40)) ;
g.drawString(file.getName().substring(0, 5), image.getWidth()-115, image.getHeight());
try
{
ImageIO.write(image, "JPEG", file);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}