码迷,mamicode.com
首页 > 其他好文 > 详细

把一张合成图分拆出各个小图

时间:2014-08-27 12:31:57      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:android   blog   http   os   java   io   for   ar   2014   

反编译一个程序,看到一张合成图,想分拆出里面的每个小图,知道了各个图的坐标大小后,写了一个小方法,希望对大家有用哈

package com.bitimage;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;

public class BigImageToSmall extends Activity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		int[] ix = { 0, 0, 32, 64, 96, 128, 168, 208, 248, 0, 33, 66, 99, 132,
				150, 168, 132, 150, 168, 186, 204, 222, 186, 204, 222, 0, 0, 0,
				240, 240, 260, 260, 0, 31, 95, 159, 223, 700, 670, 670, 287, 0,
				260, 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 0, 670, 670, 0, 0,
				626, 648, 0, 298, 0, 288, 320, 0, 0, 670, 702, 734, 670, 702,
				734, 670, 702, 734, 670, 120, 130, 140, 150, 160, 170, 180,
				190, 200, 210, 702, 0, 0, 0, 0, 0, 320, 0 };
		int[] iy = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 42, 42, 42, 40, 40, 40, 58,
				58, 58, 40, 40, 40, 58, 58, 58, 76, 127, 144, 40, 57, 231, 248,
				280, 280, 280, 280, 280, 295, 234, 295, 280, 321, 153, 153,
				153, 153, 153, 153, 153, 153, 153, 153, 153, 355, 0, 139, 504,
				225, 0, 0, 170, 40, 425, 0, 263, 0, 0, 333, 333, 333, 380, 380,
				380, 427, 427, 427, 474, 153, 153, 153, 153, 153, 153, 153,
				153, 153, 153, 474, 0, 0, 0, 0, 0, 0, 459 };
		int[] iw = { 0, 32, 32, 32, 32, 40, 40, 40, 40, 33, 33, 33, 33, 18, 18,
				18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 290, 320, 294, 58, 48,
				58, 58, 31, 64, 64, 64, 64, 44, 80, 30, 31, 320, 50, 12, 12,
				12, 12, 12, 12, 12, 12, 12, 12, 310, 113, 110, 308, 260, 22,
				22, 260, 17, 320, 28, 202, 1, 1, 32, 32, 32, 32, 32, 32, 32,
				32, 32, 32, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 76, 1, 1,
				1, 1, 1, 306, 320 };
		int[] ih = { 0, 42, 42, 42, 42, 35, 35, 35, 35, 34, 34, 34, 34, 18, 18,
				18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 51, 17, 9, 17, 9, 17,
				17, 41, 41, 41, 41, 41, 24, 61, 32, 41, 34, 78, 17, 17, 17, 17,
				17, 17, 17, 17, 17, 17, 70, 139, 95, 40, 55, 520, 520, 55, 20,
				34, 36, 57, 1, 1, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 17,
				17, 17, 17, 17, 17, 17, 17, 17, 17, 48, 1, 1, 1, 1, 1, 263, 45 };

		System.out.println("ix length = " + ix.length);
		System.out.println("iy length = " + iy.length);
		System.out.println("iw length = " + iw.length);
		System.out.println("ih length = " + ih.length);
		FileOutputStream fos = null;
		for (int i = 1; i < 95; i++) {
			System.out.println(i);
			String aa = String.format("a%02d", i);
			File file = new File(Environment.getExternalStorageDirectory(), aa + ".png");
//			File file = new File(System.getProperty("user.dir"),"a"
//					+ i + ".png");
			System.out.println(file);

			if (!file.exists()) {
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			Drawable d = getResources().getDrawable(R.drawable.cs_jmp);
			BitmapDrawable bd = (BitmapDrawable) d;
			Bitmap bm = bd.getBitmap();
			Bitmap bitmap = Bitmap.createBitmap(bm, ix[i], iy[i], iw[i], ih[i]);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			bitmap.compress(CompressFormat.PNG, 100, baos);
			InputStream in = new ByteArrayInputStream(baos.toByteArray());

			try {
				fos = new FileOutputStream(file);
				byte[] bytes = new byte[100];
				int tempbyte;
				while ((tempbyte = in.read(bytes)) != -1) {
					System.out.println("tempbyte = " + tempbyte);
					fos.write(bytes,0,tempbyte);
				}
				fos.flush();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (fos != null) {
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}

  记得加权限哦

<uses-permission
		android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  demo图片

bubuko.com,布布扣

把一张合成图分拆出各个小图

标签:android   blog   http   os   java   io   for   ar   2014   

原文地址:http://www.cnblogs.com/ycclmy/p/3939125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!