当前位置: 代码迷 >> J2SE >> 能不能帮小弟我修改一个java 切图的程序
  详细解决方案

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

热度:117   发布时间:2016-04-24 13:34:53.0
能不能帮我修改一个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)
  相关解决方案