java圖像分割方法集合_第1頁
java圖像分割方法集合_第2頁
java圖像分割方法集合_第3頁
java圖像分割方法集合_第4頁
java圖像分割方法集合_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、class ImageTest extends JFrame public ImageTest() setSize(300,300); setVisible(true); public Image splitImage(String file, int rows, int cols) Image t=new ImageIcon(file).getImage(); Image result = new Imagerows * cols; int w = t.getWidth(this)/cols; int h = t.getHeight(this)/rows; try for(int i = 0

2、;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); catch(Exception e) return result; class Test extends JFrame public Test() setSize(300,300); setVisible(true); public void paint(Graphics g) Image

3、Test 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(); 將一個大圖像分割成幾個小圖像的代碼/分割圖像的方法。 public class ChenWin504 extends JApplet Image im; MediaTracker me; Image imag; public static Hashtable cache; publi

4、c void init() imag=new Image10; 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-"+i+".png"); p

5、ublic 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() File dir = new File(url.getFile(); if (dir != null && dir.isDirectory() String list = dir.list(); cache =

6、new Hashtable(list.length); for (int i = 0; i < list.length; i+) cache.put(listi, createImage(listi, this); if (cache.containsKey("jumptojavastrip.png") Image img = (Image) cache.get("jumptojavastrip.png"); for (int i=0, x=0; i < 10; i+, x+=80) String s = "jumptojavast

7、rip-" + String.valueOf(i) + ".png" cache.put(s, getCroppedImage(img, x, 0, 80, 80, this); public static Image createImage(String fileName, Component cmp) Image img = cmp.getToolkit().createImage(url); trackImage(img, cmp); return img; /分割圖像的方法。 public static Image getCroppedImage(Imag

8、e img, int x, int y, int w, int h, Component cmp) 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 croppedim

9、age; private static void trackImage(Image img, Component cmp) MediaTracker tracker = new MediaTracker(cmp); tracker.addImage(img, 0); try tracker.waitForID(0); catch (Exception ex) ex.printStackTrace(); 效果圖:java 圖片切割,縮放,轉(zhuǎn)換類型等等 public class ImageCut /* * 縮放圖像 * param srcImageFile源圖像文件地址 * param resul

10、t縮放后的圖像地址 * 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 (f

11、lag) / 放大 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(

12、); 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(St

13、ring 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 > de

14、stHeight) Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); destWidth = 200; / 切片寬度 destHeight = 150; / 切片高度 int cols = 0; / 切片橫向數(shù)量 int rows = 0; / 切片縱向數(shù)量 / 計算切片的橫向和縱向數(shù)量 if (srcWidth % destWidth = 0) cols = srcWidth / destWidth; else cols = (int) Math.floor(srcWidth / des

15、tWidth) + 1; if (srcHeight % destHeight = 0) rows = srcHeight / destHeight; else rows = (int) Math.floor(srcHeight / destHeight) + 1; / 循環(huán)建立切片 / 改進的想法:是否可用多線程加快切割速度 for (int i = 0; i < rows; i+) for (int j = 0; j < cols; j+) / 四個參數(shù)分別為圖像起點坐標和寬高 / 即: CropImageFilter(int x,int y,int width,int hei

16、ght) 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.getGraphic

17、s(); g.drawImage(img, 0, 0, null); / 繪制縮小后的圖 g.dispose(); / 輸出為文件 ImageIO.write(tag, "JPEG", new File(descDir + "pre_map_" + i + "_" + j + ".jpg"); catch (Exception e) e.printStackTrace(); / 圖像類型轉(zhuǎn)換GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X) public stati

18、c 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(); / 彩色轉(zhuǎn)為黑白 public static void gra

19、y(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

20、) e.printStackTrace(); public static void main(String args) /cut("e:/1.jpg", "e:/t/", 200, 150); 求助大圖像分割并輸出分割后的小圖,程序有問題,求教程序要求就是,把一幅1620x1620的大圖片分割成20x20個 81x81的小圖片,并把小圖片以PNG格式輸出,源碼如下,需輸入三個參數(shù):Inputimagefile XCell YCell 2.jpg 20 20源碼不完全,現(xiàn)在暫時先計劃分割大圖第一個橫軸的長條,因為運行到ImageIO.write(CellI

21、mage, "png", new File("2"+spliter+i+spliter+"0"+outFileName);這行時就有問題。請求教哪里出了問題,急/package imageslicer;public class test private static BufferedImage CellImage; public static void main(String args) throws Exception / make sure we have exactly two arguments, / a waiting pe

22、riod and a file name if (args.length != 3) "Inputimagefile XCell YCell"); System.exit(1); String inputFileName = args0; Toolkit toolkit = Toolkit.getDefaultToolkit(); Image image = toolkit.getImage(inputFileName); MediaTracker tracker = new MediaTracker(new Container(); tracker.addImage(im

23、age, 1); try tracker.waitForID(1); catch(InterruptedException e) String outFileName = ".png" String spliter ="-" int XCell = Integer.parseInt(args1); int YCell = Integer.parseInt(args2); /ImageProducer producer = image.getSource(); /BufferedImage CellImage = new BufferedImage(81,

24、81, BufferedImage.TYPE_INT_RGB); Image Cell = new Image XCell ; for (int i = 0; i <= XCell-1; i+) ImageFilter filter = new CropImageFilter(0+i * 81, 0, 81, 81); ImageProducer producer=new FilteredImageSource(image.getSource(),filter); /Celli = createImage(new FilteredImageSource(producer, filter)

25、; Celli = createImage(FilteredImageSource) producer); CellImage = (BufferedImage) Celli; tracker.addImage(Celli, 2); /CellImage = ImageIO.read(ImageInputStream) Celli); BufferedImage CellImage = new BufferedImage(81,81, BufferedImage.TYPE_INT_RGB); CellImage = (BufferedImage) Celli; ImageIO.write(Ce

26、llImage, "png", new File("2"+spliter+i+spliter+"0"+outFileName); try tracker.waitForID(2); catch(InterruptedException e) /BufferedImage image = robot.createScreenCapture(screenRect); public void paint(Graphics g) Graphics2D g2 = (Graphics2D) g; private static Image crea

27、teImage(FilteredImageSource source) / TODO Auto-generated method stub return null; 問題解決了,原因如下切割圖片并輸出時,不能用下面的方法,下面的方法是適合在屏幕上顯示或者copy到內(nèi)存里供其它使用 ImageFilter filter = new CropImageFilter(0+i * 81, 0, 81, 81); ImageProducer producer=new FilteredImageSource(image.getSource(),filter); /Celli = createImage(n

28、ew FilteredImageSource(producer, filter); Celli = createImage(FilteredImageSource) producer);而要用getSubimage 方法,這才是適合成圖輸出的方法BufferedImage CellImage = new BufferedImage(newFile(.);CellImage.getSubimage(0+i * 81, 0, 81, 81);用Java簡單實現(xiàn)文件分割與合并,主要應用IO的RandomAccessFile(聽說斷點續(xù)傳是用它實現(xiàn))class Fen String fileName;

29、 int size; Fen(String fileName,String size) this.fileName = fileName; this.size = Integer.parseInt(size)*1024; public void cut()throws Exception int maxx = 0; File inFile = new File(fileName); int fileLength = (int)inFile.length(); /取得文件的大小int value; /取得要分割的個數(shù) RandomAccessFile inn = new RandomAccess

30、File(inFile,"r");/打開要分割的文件 value = fileLength/size; int i=0; int j=0; /根據(jù)要分割的數(shù)目輸出文件 for (;j File outFile = new File(inFile.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(); outt.close();inn

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論