[JC]ログイン画面用の画像分割プログラム


_1247611434908画像は開発途中のログイン画面のものなのであまり気にしないで下さい^^;

今回作成したのは、mixiであるような大きな画像を等分割して体感速度を上げるための小細工用のプログラムです。単純に指定されたサイズで画像を切り取っています。まぁ、画像ソフトの使い方を知らない自分としては、これしか使い道がないですし、なによりプログラムになっているので指定された材料を置けば使える状態になっているので、2、3度行う時にかなり便利です。

というか、やはりフロント側もjavaで作ることになっちゃいましたね^^;なんというか、管理側から作成するにはかなりの根気がないとモチベーション的に持たないことが分かりましたw

プログラムは詳細以下に載せておきたいと思います。

広告

■org.fukata.jc_tools.main.ImageDiviveMain

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.fukata.jc_tools.main;
import java.awt.image.BufferedImage;
import java.io.File;
 
import javax.imageio.ImageIO;
 
import org.fukata.jc_common.utils.ImageUtils;
 
 
/**
 * ログイン画面用の950x460画像を横5, 縦2に分割して生成する
 * 
 * @author tatsuya
 *
 */
public class ImageDiviveMain {
 
	private static final int WIDTH=950;
	private static final int HEIGHT=460;
 
	private static final int IMG_WIDTH=190;
	private static final int IMG_HEIGHT=230;
 
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		String workDir = "/home/tatsuya/workspace/workspace_java_ee/ImageDivideTools/src/main/resources";
		String filename = workDir+"/main.png";
		BufferedImage loadImage = ImageUtils.loadImage(filename);
 
		int width = loadImage.getWidth();
		int height = loadImage.getHeight();
		System.out.println("width="+width);
		System.out.println("height="+height);
 
		// check
		if (WIDTH!=width || HEIGHT!=height) {
			throw new Exception("width="+width+", height="+height);
		}
 
		// subimage
		int count = 0;
		for (int i=0; i<height /IMG_HEIGHT; i++) {
			for (int j=0; j<WIDTH/IMG_WIDTH; j++) {
				count++;
				System.out.println(String.format("x=%03d, y=%03d, w=%03d, h=%03d", j*IMG_WIDTH, i*IMG_HEIGHT, IMG_WIDTH, IMG_HEIGHT));
				BufferedImage image = loadImage.getSubimage(j*IMG_WIDTH, i*IMG_HEIGHT, IMG_WIDTH, IMG_HEIGHT);
 
				// write
				String imgName = String.format("main_%02d.png", count);
				ImageIO.write(image, "png", new File(workDir+"/result/"+imgName));
			}
		}
	}
 
}

■org.fukata.jc_common.utils.ImageUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package org.fukata.jc_common.utils;
 
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
 
import javax.imageio.ImageIO;
 
public class ImageUtils {
	public static BufferedImage loadImage(String filename) {
		InputStream inputStream = null;
		BufferedImage read = null;
		try {
			inputStream = new FileInputStream(filename);
			read = ImageIO.read(inputStream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (inputStream!=null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
 
		return read;
	}
}

関連記事