版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
importjava?awt?*;importjavax?swing??:classImageTestextendsJFrame{publicImageTest(){setSize(300,300);setVisible(true);}publicImage[]splitImage(Stringfile,introws,intcols)System?out?printlnC^Test");Imaget=newImageicon(file)?getImage():Imagedresult=newImage[rows*cols]:intw=t.getWidth(this)/cols;inth=t.getHeight(this)/rows;try{for(inti=0;i<result.1ength;i++){result[i]=this?createImage(w.h);Graphicsg=result[i]?getGraphics():g.translate((-i%cols)*w.(-i/cols)*h);g.drawlmage(t,0.0.this);System?out?println(wTestH);}}catch(Exceptione){}returnresult;}}classTestextendsJFrame{publicTest(){setSize(300,300);setVisible(true);}publicvoidpaint(Graphicsg){ImageTesttt=newImageTest():Imagedimage=tt?splitlmage(u1?gif".5.5);g.drawlmage(image[01,0.0.this);}publicstaticvoidmain(String[]args){newTest();}}將一個大圖像分割成幾個小圖像的代碼〃分割圖像的方法。importjavax.swing?*; importjava?awt?*;importjava?awt?image?*;importjava.net.URL; importjava?io.File;importjava.util.Hashtable;publicclassChenWin504extendsJApplet{Imageim;MediaTrackerme;Imageimag[]:publicstaticHashtablecache;publicvoidinit(){URLur=ChenWin504.class?getResource(uimages/ima);imag=newImage[10]:System.out?println(ur);im-getToolkit()?getImage(nimages/ima/jumptojavastrip?png");me=newMediaTracker(this):me.addlmage(im?0);try{me.waitForlD(O);}catch(Exceptione){}DemoImagesO;for(inti=0;i<10;i++){imag[i]=(Image)cache?get(wjumptojavastrip-rT+i+,'?png");if(imag[i]==nul1)System?out?println(Hnul1:publicvoidpaint(Graphicsg){intx=5;inty=100;g.drawlmage(im,5,0,null);for(inti二0;i<10;i++){g.drawlmage(imag[i],xfy,null);x+=100;if(x>300){x二5;y+=100;}}publicvoidDemoImagesO{URLurl=ChenWinSO4.class?getResource("iniages/ini3");Filedir=newFile(ur1.getFileO):if(dir!=null&&dir.isDirectory()){Stringlist[]=dir.list():System?out?println(list?length);cache=newHashtable(1ist.1ength):for(inti=0;i<list,length;i++){cache.put(1ist[i]tcreateImage(1ist[i],this)):}}if(cache?containsKey(wjumptojavastrip?png")){Imageimg=(Image)cache?get("jumptojaYsstrip?png");for(inti二0.x二0;i<10;i++.x+二80){Strings=Hjumptojavastrip-'*+String.valueOf(i)+png";cache.put(s,getCroppedlmage(img,x,0,80,80,this)):}}}publicstaticImagecreatelmage(StringfileName,Componentcmp){URLurl=ChenWin504?class?getResource("images/inia/"+fi1eName):Imageimg=cmp?getToolkit()?createlmage(url):trackimage(img,cmp);returnimg;}〃分割圖像的方法。publicstaticImagegetCroppedImage(Imageimg,intx,inty,intw,inth,Componentcmp){ImageProducerimgP=img?getSourceO;CropImageFiltercif=newCropImageFi11er(x,y,w,h):ImageProducerip=newFi11ei^edImageSource(imgP,cif);Imagecropped]mage=cmp?getToolkit()?createlmage(ip):trackImage(croppedimage,cmp);returncroppedimage;}privatestaticvoidtrackImage(Imageimg?Componentcmp){MediaTrackertracker=newMediaTracker(cmp):tracker?addImage(img,0);try{tracker,waitForID(0);}catch(Exceptionex){ex.printStackTraceO:}}}效果圖:java圖片切割?縮放,轉(zhuǎn)換類型等等importjava?io.*; importjava?awt?*;importjava?awt?image?*;importjava.awt.Graphics;importjava?awt.color.ColorSpsce;importjavax?imageio.ImagelO;publicclassImageCut{/?**縮放圖像?paramsrcImageFile源圖像文件地址?paramresult縮放后的圖像地址?paramscale縮放比例?paramflag縮放選擇:true放大;false縮??;?/publicstaticvoidscale(StringsrcImageFile,Stringresult,intscale,booleanflag){try{Bufferedlmagesrc=ImagelO.read(newFile(srcImageFile))://讀入文件intwidth=src.getWidthO;//得到源圖寬intheight=src.getHeight()://得到源圖長if(flag){//放大width=width*scale;height=height*scale;}else{//縮小width=width/scale;height=height/scale;}Imageimage=src?getScaledlnstance(width.height,Image.SCALE_DEFAULT);Bufferedlmagetag=newBufferedlmage(width,height,BufferedImage.TYPE^INTRGB);Graphicsg=tag.getGrsphics();g.drawImage(image,0,0,nul1)://繪制縮小后的圖g.disposeO;ImagelO.write(tag,MJPEGWFnewFile(result))://輸出到文件流}catch(IOExceptione){e.printStackTraceO;}}***圖像切割?paramsrcImageFile源圖像地址?paramdescDir切片目標文件夾?paramdestWidth目標切片寬度?paramdestHeight目標切片高度*/publicstaticvoidcut(StringsrcImageFile,StringdescDir,intdestWidth,intdestHeight){try{Imageimg;ImageF訂tercropFiIter;//讀取源圖像BufferedImagebi=Image10.read(newFile(srcImageFile)):intsrcWidth=bi.getHeight();//源圖寬度intsrcHeight=bi.getWidthO://源圖高度if(srcWidth>destWidth&&srcHeight>destHeight){Imageimage=bi.getScaledlnstance(srcWidth,srcHeight,Image.SCALE_DEFAULT):destWidth=200;//切片寬度destHeight=150;//切片高度intcols=0;//切片橫向數(shù)量introws=0;//切片縱向數(shù)量//計算切片的橫向和縱向數(shù)量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:}//循環(huán)建立切片//改進的想法:是否可用多線程加快切割速度for(inti=0;i<rows;i++){for(intj=0;j<cols;j卄){//四個參數(shù)分別為圖像起點坐標和寬高//即:CropImageFiIter(intx,inty,intwidth,intheight)cropFilter=newCropImageFi11er(j*200.i*150,destWidth,destHeight);img=Toolkit.getDefaultToolkit()?createlmage(newFi1teredlmageSource(image?getSourceO,cropFiIter));BufferedImagetag=newBuffei'edImage(destWidthtdestHeight,Bufferedlmage.TYPEJNT_RGB);Graphicsg=tag?getGraphics();g.drawlmage(imgt0,0,null)://繪制縮小后的圖g.disposeO;//輸出為文件ImagelO.write(tag,"JPEG”,newFile(descDir+^pre,map"+i+ +j+"?jpg"));}}}}catch(Exceptione){printStackTraceO;}}//圖像類型轉(zhuǎn)換GIF-〉JPGGIF->PNGPNG->JPGPNG->GIF(X)publicstaticvoidconvert(Stringsource,Stringresult){try{Filef=newFile(source):canReadO;f.canWrite();Bufferedlmagesrc=Image10.read(f):Image10.write(srct"JPG",newFile(result)):}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTraceO;}}//彩色轉(zhuǎn)為黑白publicstaticvoidgray(Stringsource.Stringresult){try{Bufferedlmagesrc=Image10.read(newFile(source)):ColorSpacecs=ColorSpace.getInstonce(ColorSp3ce?CSGRAY);ColorConvertOpop=newColorConvert0p(cs,null);
src=op.filter(src.null);Image10.write(src,"JPEGJnewFile(result)):}catch(IOExceptione){e.printStackTraceO;}}publicstaticvoidmain(StringE]args){//cut(”e:/l?jpg", 200t150);}}[求助]大圖像分割并輸出分割后的小圖,程序有問題,求教程序要求就是,把一幅1620x1620的大圖片分割成20x20個81x81的小圖片,并把小圖片以P7G格式輸出,源碼如下,需輸入三個參數(shù):InputimagefileXCellYCell一一》2.jpg2020源碼不完全,現(xiàn)在暫時先計劃分割大圖第一個橫軸的長條,因為運行到Imagel0.write(Cellimage, HpngM, newFileC^^+spliter+i+spliter+,fOn+outFileName));這行時就有問題。請求教哪里出了問題,急??//packageimageslicer;publicclasstest{importimportimportimportimportimportimportimportimportimportjava.awt?Container;importimportimportimportimportimportimportimportimportimportjava.awt?Container;java.awt.Graphics;java.awt?Image;java.awt?Rectangle;java.awt?Robot;importimportimportimportimportjava.awt?Dimension;java.awt?Graphics2D;java.awt.MediaTracker;java.awt?RenderingHints;java?awt?Toolkit;importjava?awt?inuige?CropImageFi1ter;java.awt?image?BufferedImage;java.awt?image?Fi11eredImageSource; importjava?awt?image?ImageFiIter;java.owt?image?ImageProducer; importjava.awt.image?RenderedIjava.io?File; importjavax?inuigeio.ImagelO;javax?imaigeio.stream.ImagelnputStream; importjavax?swing?Imsgelcon;privatestaticBufferedImageCel1Image;publicstaticvoidmain(StringEJargs)throwsException{//makesurewehaveexactlytwoarguments,//awaitingperiodandafilenameif(args,length!=3){System.err?println("Us3ge:javatest”+HInputimagefileXCellYCell");System?exit(1);StringinputFileName=args[O]:Toolkittoolkit=Toolkit.getDefaultToolkit():Imageimage=toolkit?getImage(inputFileName):MediaTrackertracker=newMediaTracker(newContainer()):tracker?addImage(image,1);try{tracker?waitForlD(l);}catch(InterruptedExceptione){}StringoutFileName=u.png";Stringspliter=°-";intXCel1=Integer?parselnt(args[l]);intYCel1=Integer?parselnt(args[2]);//ImageProducerproducer=image?getSource();//Bufferedlmage CellImage=newBufferedlmage(81.81.Bufferedlmage.TYPE^INTRGB):Image[]Cell=newImageXCell;for(inti=0;i<=XCell-1;i++){ImageFilterfilter=newCropImageFiIter(0+i*81.0,81,81):ImageProducer producer=newFi1teredImageSource(image?getSourceO.filter);//Cel1[i]=createImage(newFiIteredlmageSource(producer,filter)):Cel1[i]=createlmage((Fi1teredlmageSource)producer):CellImage=(Bufferedlmage)Cell[i];tracker?addImage(Cell[i].2)://CellImage=ImagelO.read((ImagelnputStream)Cell[i]):Bufferedlmage Cellimage=new Bufferedlmage(81.81.Bufferedlmage.TYPE^INTRGB):CellImage=(Bufferedlmage)Cell[i];Image10.write(CellImage, "png". newF訂e(M2M+spliter+i+spliter+w0M+outFileName));}try{tracker,waitForID(2);}catch(InterruptedExceptione){}//Bufferedlmageimage=robot?cresteScreenCapture(screenRect);}publicvoidpaint(Graphicsg){Graphics2Dg2=(Graphics2D)g;}privatestaticImagecreatelmage(Fi1teredlmageSourcesource){//TODOAuto-generatedmethodstubreturnnull;}}問題解決了,原因如下切割圖片并輸出時,不能用下面的方法,下面的方法是適合在屏幕上顯示或者copy到內(nèi)存里供其它使用ImageFilterfilter=newCropImageFiIter(0+i*81,0,81,81);ImageProducer producer=newFi1teredImageSource(image?getSourceO.filter);//Cel1[i]=createImage(newFi1teredImageSource(producer,filter)):Cel1[i]=createlmage((Fi1teredlmageSource)producer):而要用getSubimage方法,這才是適合成圖輸出的方法BufferedImageCellImage=newBufferedImage(newFi1e( ));Celllmage.getSubimage(0+i*81#0,81t81);用Java簡單實現(xiàn)文件分割與合并,主要應用10的RandomAccessFile(聽說斷點續(xù)傳是用它實現(xiàn))importjava?io?*;classFen{Stringfi1eName;intsize;Fen(StringfileName,Stringsize){this?fileNsme=fileName;this.size=Integer?parselnt(size)*1024;}publicvoidcut()throwsExceptiontintmaxx=0;FileinFile=newFile(fileName):intfileLength=(int)inFile,length()://取得文件的大小intvalue;〃取得要分割的個數(shù)RandomAccessFi1einn=newRandomAccessF訂e(inF訂e,U;〃打開要分割的文件value=fileLength/size;inti=0;intj=0;〃根據(jù)要分割的數(shù)目輸出文件for(:jFileoutFile=newFile(inFile?getName()+j+"zzii");RandomAccessFileoutt=newRandom/\ccessFile(outFile,nrwn):maxx+=size;for(;ioutt.write(inn.readO):}outt?closeO;}FileoutFile=newFile(inFile?get'mne()+j+"zzii");RandomAccessFileoutt=newRandom/\ccessFile(outFile,nrwn):for(:i>;)outt?writednn?read());}outt?closeO;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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度新能源汽車充電樁建設合作合同
- 2024房地產(chǎn)項目投資追加與調(diào)整合同
- 2024年中國高效無阻塞藕筒輥市場調(diào)查研究報告
- 2024版雙邊技術研發(fā)合作合同
- 2024深圳商鋪租賃合同范本:裝修、維修及物業(yè)管理條款
- 2025年度學校臨時教科研人員合作協(xié)議3篇
- 2024年中國鑲人造寶石項鏈市場調(diào)查研究報告
- 2024年社保自愿放棄聲明書(個人版)3篇
- 2024渣土運輸及資源化利用項目承包合同3篇
- 2024年租車期間的保險責任2篇
- 公務員調(diào)任(轉(zhuǎn)任)審批表 - 陽春人才網(wǎng)
- IE部成立工作規(guī)劃
- 單體調(diào)試及試運方案
- 2023-2024學年浙江省杭州市城區(qū)數(shù)學四年級第一學期期末學業(yè)水平測試試題含答案
- 網(wǎng)球技術與戰(zhàn)術-華東師范大學中國大學mooc課后章節(jié)答案期末考試題庫2023年
- 2023年35kV集電線路直埋施工方案
- 思政教師培訓心得體會2021
- HLB值的實驗測定方法
- 2023年《病歷書寫基本規(guī)范》年度版
- 防止電力生產(chǎn)事故的-二十五項重點要求2023版
- 代理記賬機構代理記賬業(yè)務規(guī)范
評論
0/150
提交評論