我 要 将一个大图 切成小图片 部分代码如下 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 是上面两个循环里面的自增变量
要是这些要求麻烦的话 就不要了
不管什么办法 只要切出来的文件名 的命名不要变就行
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 是上面两个循环里面的自增变量
要是这些要求麻烦的话 就不要了
不管什么办法 只要切出来的文件名 的命名不要变就行
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";
我邮箱 [email protected]
不胜感激啊
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
在.net里面加个按钮,调用这个函数就行了.
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 = Nothing intColumns = Math.Floor(imgPhoto.Width / Tile_Width)
intRows = Math.Floor(imgPhoto.Height / Tile_Height) For i = 0 To intColumns
For j = 0 To intRows
Tile_Width = 256
Tile_Height = 256
If (i + 1) * 256 > imgPhoto.Width Then Tile_Width = imgPhoto.Width - i * 256
If (j + 1) * 256 > imgPhoto.Height Then Tile_Height = imgPhoto.Height - j * 256
bmPhoto = New Bitmap(Tile_Width, Tile_Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
gbmPhoto = Graphics.FromImage(bmPhoto)
gbmPhoto.DrawImage(imgPhoto, New Rectangle(0, 0, Tile_Width, Tile_Height), _
CInt(i * 256), CInt(j * 256), _
CInt(Tile_Width), CInt(Tile_Height), _
GraphicsUnit.Pixel) bmPhoto.Save(vPath & i.ToString & "," & j.ToString & ".jpg")
bmPhoto.Dispose()
gbmPhoto.Dispose()
Next
Next imgPhoto.Dispose()
End Sub