画像分割

1586 ワード

まず、小さなブロック画像のクラスを作成し、切断後の画像を保存します.
public class ImagePiece {

	private int index;

	private Bitmap bitmap;

	public ImagePiece()

	{}

	public ImagePiece(int index,Bitmap bitmap) {

		this.index=index;

		this.bitmap=bitmap;

	}

	

	public int getIndex() {

		return index;

	}

	

	public void setIndex(int index) {

		this.index = index;

	}

	

	public Bitmap getBitmap() {

		return bitmap;

	}

	

	public void setBitmap(Bitmap bitmap) {

		this.bitmap = bitmap;

	}

	

	@Override

	public String toString() {

		return "ImagePiece [index=" + index + ", bitmap=" + bitmap

				+ ", toString()=" + super.toString() + "]";

	}



}


画像をカット
/**

	 *  bitmap 

	 * @param bitmap

	 * @param piece

	 * @return List<ImagePiece>

	 */

	public static List<ImagePiece> splitImage(Bitmap bitmap,int piece)

	{

		

		List<ImagePiece> imagePieces=new ArrayList<ImagePiece>();

		

		

		int width=bitmap.getWidth();

		int height=bitmap.getHeight();

		

		int pieceWidth=Math.min(width, height)/piece;

		

		

		for(int i=0;i<piece;i++)

		{

			for(int j=0;j<piece;j++)

			{

				ImagePiece imagePiece=new ImagePiece();

				imagePiece.setIndex(j+i*piece);

				

				int x=j*pieceWidth;

				int y=i*pieceWidth;

				Log.i("x", x+"");

				Log.i("y", y+"");

				imagePiece.setBitmap(Bitmap.createBitmap(bitmap, x, y, pieceWidth, pieceWidth));

				

				imagePieces.add(imagePiece);

				

			}

		}

		return imagePieces;

	}