首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

能不能帮小弟我修改一个java 切图的程序

2011-12-19 
能不能帮我修改一个java 切图的程序我 要 将一个大图 切成小图片部分代码如下package com.ajaxian.amapsi

能不能帮我修改一个java 切图的程序
我 要 将一个大图 切成小图片 

部分代码如下 

package com.ajaxian.amaps; 

import org.apache.batik.apps.rasterizer.DestinationType; 
import org.apache.batik.apps.rasterizer.SVGConverter; 

import javax.imageio.ImageIO; 
import java.io.File; 
import java.awt.*; 
import java.awt.image.BufferedImage; 

public class ImageTiler { 
  private static final String BASE_DIR = "resources/"; 
  private static final int TILE_WIDTH = 256; 
  private static final int TILE_HEIGHT = 256; 

  public static void main(String[] args) throws Exception { 
  // create the tiles 
  String[][] sources = { { "tiles/mapSpain.jpg", "1" }, 
  {"tiles/mapSpain-smaller.jpg", "2"} }; 
  for (int i = 0; i < sources.length; i++) { 
  String[] source = sources[i]; 
  BufferedImage bi = ImageIO.read(new File(BASE_DIR + source[0])); 
  int columns = bi.getWidth() / TILE_WIDTH; 
  int rows = bi.getHeight() / TILE_HEIGHT; 
  for (int x = 0; x < columns; x++) { 
  for (int y = 0; y < rows; y++) { 
  BufferedImage img = new BufferedImage(TILE_WIDTH, TILE_HEIGHT, 
  bi.getType()); 
  Graphics2D newGraphics = (Graphics2D) img.getGraphics(); 
  newGraphics.drawImage(bi, 0, 0, TILE_WIDTH, TILE_HEIGHT, 
  TILE_WIDTH * x, TILE_HEIGHT * y, 
  TILE_WIDTH * x + TILE_WIDTH, 
  TILE_HEIGHT * y + TILE_HEIGHT, 
  null); 
  ImageIO.write(img, "JPG", new File(BASE_DIR + "tiles"+source[1]+"/" + 
  "x" + x + "y" + y + "z" + ".jpg")); 
  } 
  } 
  } 
  } 



这是一个将大图切成小图的程序 
我现在要一个界面 能选择文件 或者直接输入路径 
要选择或输入保存路径 文件名的格式要这样子的 "x" + x + "y" + y + "z" + ".jpg")); 
x y 是上面两个循环里面的自增变量 


要是这些要求麻烦的话 就不要了  
不管什么办法 只要切出来的文件名 的命名不要变就行 


[解决办法]
for example

open file
JFileChooser fc = new JFileChooser("yourpath");
fc.setFileSelectionMode(JFileChooser.FILE_ONLY);
fc.setMultiSelectionEnabled(false);
fc.showOpenDialog(this);
File f = fc.getSelectedFile();

save file
JFileChooser fc = new JFileChooser("yourpath");
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
fc.setMultiSelectionEnabled(false);
fc.showSaveDialog(this);
String fn = fc.getSelectedFile().getAbsolutePath() + "x" + x + "y" + y + "z" + ".jpg";


[解决办法]
Private Sub SplitImage(ByVal vPath As String, ByVal vFile As String)
Dim Tile_Width As Integer = 256
Dim Tile_Height As Integer = 256
Dim i, j As Integer
Dim intColumns, intRows As Integer

Dim gbmPhoto As Graphics = Nothing
Dim imgPhoto As Image = Image.FromFile(vPath & vFile)
Dim bmPhoto As Bitmap = New Bitmap(Tile_Width, Tile_Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)



intColumns = Math.Floor(imgPhoto.Width / Tile_Width)
intRows = Math.Floor(imgPhoto.Height / Tile_Height)

For i = 0 To intColumns
For j = 0 To intRows
gbmPhoto = Graphics.FromImage(bmPhoto)
gbmPhoto.DrawImage(imgPhoto, i * Tile_Width, j * Tile_Height, Tile_Width, Tile_Height)
gbmPhoto.DrawImage(imgPhoto, New Rectangle(0, 0, Tile_Width, Tile_Height), _
CInt(i * Tile_Width * imgPhoto.Width / imgPhoto.Width), CInt(j * Tile_Height * imgPhoto.Height / imgPhoto.Height), _
CInt(Tile_Width * imgPhoto.Width / imgPhoto.Width), CInt(Tile_Height * imgPhoto.Height / imgPhoto.Height), _
GraphicsUnit.Pixel)

bmPhoto.Save(vPath & i.ToString & "," & j.ToString & ".jpg")
Next
Next

imgPhoto.Dispose()
gbmPhoto.Dispose()
bmPhoto.Dispose()
End Sub

热点排行
Bad Request.