java图像分割方法集合_第1页
java图像分割方法集合_第2页
java图像分割方法集合_第3页
java图像分割方法集合_第4页
java图像分割方法集合_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、import java.awt.*;import javax.swing.*;class ImageTest extends JFrame public ImageTest() setSize(300,300); setVisible(true); public Image splitImage(String file, int rows, int cols) System.out.println("Test");Image t=new ImageIcon(file).getImage(); Image result = new Imagerows * cols; int

2、w = t.getWidth(this)/cols; int h = t.getHeight(this)/rows; try for(int i = 0;i<result.length;i+) resulti = this.createImage(w,h); Graphics g = resulti.getGraphics(); g.translate(-i%cols)*w,(-i/cols)*h); g.drawImage(t,0,0,this); System.out.println("Test"); catch(Exception e) return resul

3、t; class Test extends JFrame public Test() setSize(300,300); setVisible(true); public void paint(Graphics g) ImageTest tt = new ImageTest(); Image image = tt.splitImage("1.gif",5,5); g.drawImage(image0,0,0,this); public static void main(String args) new Test(); 将一个大图像分割成几个小图像的代码/分割图像的方法。 i

4、mport javax.swing.*; import java.awt.*; import java.awt.image.*;import .URL; import java.io.File;import java.util.Hashtable; public class ChenWin504 extends JApplet Image im; MediaTracker me; Image imag; public static Hashtable cache; public void init() URL ur=ChenWin504.class.getResource("imag

5、es/ima"); imag=new Image10; System.out.println(ur); im=getToolkit().getImage("images/ima/jumptojavastrip.png"); me=new MediaTracker(this); me.addImage(im,0); try me.waitForID(0); catch(Exception e) DemoImages(); for(int i=0;i<10;i+) imagi=(Image)cache.get("jumptojavastrip-&quo

6、t;+i+".png"); if(imagi=null) System.out.println("null"); public void paint(Graphics g) int x=5; int y=100; g.drawImage(im,5,0,null); for(int i=0;i<10;i+) g.drawImage(imagi,x,y,null); x+=100; if(x>300) x=5;y+=100; public void DemoImages() URL url = ChenWin504.class.getResour

7、ce("images/ima"); File dir = new File(url.getFile(); if (dir != null && dir.isDirectory() String list = dir.list(); System.out.println(list.length); cache = new Hashtable(list.length); for (int i = 0; i < list.length; i+) cache.put(listi, createImage(listi, this); if (cache.cont

8、ainsKey("jumptojavastrip.png") Image img = (Image) cache.get("jumptojavastrip.png"); for (int i=0, x=0; i < 10; i+, x+=80) String s = "jumptojavastrip-" + String.valueOf(i) + ".png" cache.put(s, getCroppedImage(img, x, 0, 80, 80, this); public static Image

9、createImage(String fileName, Component cmp) URL url = ChenWin504.class.getResource("images/ima/" + fileName); Image img = cmp.getToolkit().createImage(url); trackImage(img, cmp); return img; /分割图像的方法。 public static Image getCroppedImage(Image img, int x, int y, int w, int h, Component cmp)

10、 ImageProducer imgP = img.getSource(); CropImageFilter cif = new CropImageFilter(x, y, w, h); ImageProducer ip = new FilteredImageSource(imgP, cif); Image croppedimage = cmp.getToolkit().createImage(ip); trackImage(croppedimage, cmp); return croppedimage; private static void trackImage(Image img, Co

11、mponent cmp) MediaTracker tracker = new MediaTracker(cmp); tracker.addImage(img, 0); try tracker.waitForID(0); catch (Exception ex) ex.printStackTrace(); 效果图:java 图片切割,缩放,转换类型等等 import java.io.*;import java.awt.*;import java.awt.image.*;import java.awt.Graphics;import java.awt.color.ColorSpace;impor

12、t 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(srcI

13、mageFile); / 读入文件 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

14、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源图像地址 * p

15、aram 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(); / 源图

16、宽度 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 (s

17、rcWidth % 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 (

18、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 =

19、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 + "pre_map_" + i + "_" + j + ".jpg"); catch (Exceptio

20、n 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 (Ex

21、ception 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 =

22、op.filter(src, null); ImageIO.write(src, "JPEG", new File(result); catch (IOException e) e.printStackTrace(); public static void main(String args) /cut("e:/1.jpg", "e:/t/", 200, 150); 求助大图像分割并输出分割后的小图,程序有问题,求教程序要求就是,把一幅1620x1620的大图片分割成20x20个 81x81的小图片,并把小图片以PNG格式输出,源码如下

23、,需输入三个参数:Inputimagefile XCell YCell 2.jpg 20 20源码不完全,现在暂时先计划分割大图第一个横轴的长条,因为运行到ImageIO.write(CellImage, "png", new File("2"+spliter+i+spliter+"0"+outFileName);这行时就有问题。请求教哪里出了问题,急/package imageslicer;import java.awt.Container;import java.awt.Dimension;import java.awt.Grap

24、hics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.MediaTracker;import java.awt.Rectangle;import java.awt.RenderingHints;import java.awt.Robot;import java.awt.Toolkit;import java.awt.image.BufferedImage;import java.awt.image.CropImageFilter;import java.awt.image.FilteredImageSourc

25、e;import java.awt.image.ImageFilter;import java.awt.image.ImageProducer;import java.awt.image.RenderedImage;import java.io.File;import javax.imageio.ImageIO;import javax.imageio.stream.ImageInputStream;import javax.swing.ImageIcon;public class test private static BufferedImage CellImage; public stat

26、ic void main(String args) throws Exception / make sure we have exactly two arguments, / a waiting period and a file name if (args.length != 3) System.err.println("Usage: java test " + "Inputimagefile XCell YCell"); System.exit(1); String inputFileName = args0; Toolkit toolkit = T

27、oolkit.getDefaultToolkit(); Image image = toolkit.getImage(inputFileName); MediaTracker tracker = new MediaTracker(new Container(); tracker.addImage(image, 1); try tracker.waitForID(1); catch(InterruptedException e) String outFileName = ".png" String spliter ="-" int XCell = Inte

28、ger.parseInt(args1); int YCell = Integer.parseInt(args2); /ImageProducer producer = image.getSource(); /BufferedImage CellImage = new BufferedImage(81,81, BufferedImage.TYPE_INT_RGB); Image Cell = new Image XCell ; for (int i = 0; i <= XCell-1; i+) ImageFilter filter = new CropImageFilter(0+i * 8

29、1, 0, 81, 81); ImageProducer producer=new FilteredImageSource(image.getSource(),filter); /Celli = createImage(new FilteredImageSource(producer, filter); Celli = createImage(FilteredImageSource) producer); CellImage = (BufferedImage) Celli; tracker.addImage(Celli, 2); /CellImage = ImageIO.read(ImageI

30、nputStream) Celli); BufferedImage CellImage = new BufferedImage(81,81, BufferedImage.TYPE_INT_RGB); CellImage = (BufferedImage) Celli; ImageIO.write(CellImage, "png", new File("2"+spliter+i+spliter+"0"+outFileName); try tracker.waitForID(2); catch(InterruptedException e

31、) /BufferedImage image = robot.createScreenCapture(screenRect); public void paint(Graphics g) Graphics2D g2 = (Graphics2D) g; private static Image createImage(FilteredImageSource source) / TODO Auto-generated method stub return null; 问题解决了,原因如下切割图片并输出时,不能用下面的方法,下面的方法是适合在屏幕上显示或者copy到内存里供其它使用 ImageFil

32、ter filter = new CropImageFilter(0+i * 81, 0, 81, 81); ImageProducer producer=new FilteredImageSource(image.getSource(),filter); /Celli = createImage(new FilteredImageSource(producer, filter); Celli = createImage(FilteredImageSource) producer);而要用getSubimage 方法,这才是适合成图输出的方法BufferedImage CellImage =

33、new BufferedImage(newFile(.);CellImage.getSubimage(0+i * 81, 0, 81, 81);用Java简单实现文件分割与合并,主要应用IO的RandomAccessFile(听说断点续传是用它实现)import java.io.*;class Fen String fileName; int size; Fen(String fileName,String size) this.fileName = fileName; this.size = Integer.parseInt(size)*1024; public void cut()thro

34、ws Exception int maxx = 0; File inFile = new File(fileName); int fileLength = (int)inFile.length(); /取得文件的大小int value; /取得要分割的个数 RandomAccessFile inn = new RandomAccessFile(inFile,"r");/打开要分割的文件 value = fileLength/size; int i=0; int j=0; /根据要分割的数目输出文件 for (;j File outFile = new File(inFile

35、.getName()+j+"zzii"); RandomAccessFile outt= new RandomAccessFile(outFile,"rw"); maxx+=size; for (;i outt.write(inn.read(); outt.close(); File outFile = new File(inFile.getName()+j+"zzii");RandomAccessFile outt= new RandomAccessFile(outFile,"rw"); for(;i >)outt.write(inn.read()

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论