/***在网上找了一个图片裁剪算法,当执行最后一个方法getCroppedBitmap()时报出异常信息:java.lang.IllegalArgumentException:y must be >=0,经调试发现y的确小于0,但我不知道代*码里面到底是哪里出错了。请指导问题的大侠可以指出问题,最好把完整的代码页贴出来,先感谢了!
**/
packagecom.jean.imagecroptool;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.OutputStream;importandroid.content.Context;importandroid.content.Intent;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Matrix;importandroid.graphics.Paint;importandroid.graphics.Paint.Style;importandroid.graphics.Point;importandroid.graphics.PointF;importandroid.graphics.drawable.BitmapDrawable;importandroid.os.Bundle;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.view.MotionEvent;importandroid.widget.ImageView;public class CropView extendsImageView {
Paint paint= newPaint();private int initial_size = 300;private static Point leftTop, rightBottom, center, previous;//yellow//rectangle
private static final int DRAG = 0;private static final int LEFT = 1;private static final int TOP = 2;private static final int RIGHT = 3;private static final int BOTTOM = 4;private intimageScaledWidth, imageScaledHeight;//Adding parent class constructors
publicCropView(Context context) {super(context);
initCropView();
}publicCropView(Context context, AttributeSet attrs) {super(context, attrs, 0);
initCropView();
}public CropView(Context context, AttributeSet attrs, intdefStyle) {super(context, attrs, defStyle);
initCropView();
}
@Overrideprotected voidonDraw(Canvas canvas) {super.onDraw(canvas);if (leftTop.equals(0, 0))
resetPoints();
canvas.drawRect(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y,
paint);
}
@Overridepublic booleanonTouchEvent(MotionEvent event) {int eventaction =event.getAction();switch(eventaction) {caseMotionEvent.ACTION_DOWN:
previous.set((int) event.getX(), (int) event.getY());break;caseMotionEvent.ACTION_MOVE:if(isActionInsideRectangle(event.getX(), event.getY())) {
adjustRectangle((int) event.getX(), (int) event.getY());
invalidate();//redraw rectangle
previous.set((int) event.getX(), (int) event.getY());
}break;caseMotionEvent.ACTION_UP:
previous= newPoint();break;
}return true;
}private voidinitCropView() {
paint.setColor(Color.YELLOW);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5);
leftTop= newPoint();
rightBottom= newPoint();
center= newPoint();
previous= newPoint();
}public voidresetPoints() {
center.set(getWidth()/ 2, getHeight() / 2);
leftTop.set((getWidth()- initial_size) / 2,
(getHeight()- initial_size) / 2);
rightBottom.set(leftTop.x+ initial_size, leftTop.y +initial_size);
}private static boolean isActionInsideRectangle(float x, floaty) {int buffer = 10;return (x >= (leftTop.x - buffer) && x <= (rightBottom.x +buffer)&& y >= (leftTop.y - buffer) && y <= (rightBottom.y + buffer)) ? true:false;
}private booleanisInImageRange(PointF point) {//Get image matrix values and place them in an array
float[] f = new float[9];
getImageMatrix().getValues(f);//Calculate the scaled dimensions
imageScaledWidth =Math.round(getDrawable().getIntrinsicWidth()*f[Matrix.MSCALE_X]);
imageScaledHeight=Math.round(getDrawable().getIntrinsicHeight()*f[Matrix.MSCALE_Y]);return (point.x >= (center.x - (imageScaledWidth / 2))&& point.x <= (center.x + (imageScaledWidth / 2))&& point.y >= (center.y - (imageScaledHeight / 2)) && point.y <= (center.y + (imageScaledHeight / 2))) ? true:false;
}private void adjustRectangle(int x, inty) {intmovement;switch(getAffectedSide(x, y)) {caseLEFT:
movement= x -leftTop.x;if (isInImageRange(new PointF(leftTop.x +movement, leftTop.y+movement)))
leftTop.set(leftTop.x+ movement, leftTop.y +movement);break;caseTOP:
movement= y -leftTop.y;if (isInImageRange(new PointF(leftTop.x +movement, leftTop.y+movement)))
leftTop.set(leftTop.x+ movement, leftTop.y +movement);break;caseRIGHT:
movement= x -rightBottom.x;if (isInImageRange(new PointF(rightBottom.x +movement,
rightBottom.y+movement)))
rightBottom.set(rightBottom.x+movement, rightBottom.y+movement);break;caseBOTTOM:
movement= y -rightBottom.y;if (isInImageRange(new PointF(rightBottom.x +movement,
rightBottom.y+movement)))
rightBottom.set(rightBottom.x+movement, rightBottom.y+movement);break;caseDRAG:
movement= x -previous.x;int movementY = y -previous.y;if (isInImageRange(new PointF(leftTop.x +movement, leftTop.y+movementY))&& isInImageRange(new PointF(rightBottom.x +movement,
rightBottom.y+movementY))) {
leftTop.set(leftTop.x+ movement, leftTop.y +movementY);
rightBottom.set(rightBottom.x+movement, rightBottom.y+movementY);
}break;
}
}private static int getAffectedSide(float x, floaty) {int buffer = 10;if (x >= (leftTop.x - buffer) && x <= (leftTop.x +buffer))returnLEFT;else if (y >= (leftTop.y - buffer) && y <= (leftTop.y +buffer))returnTOP;else if (x >= (rightBottom.x - buffer) && x <= (rightBottom.x +buffer))returnRIGHT;else if (y >= (rightBottom.y - buffer) && y <= (rightBottom.y +buffer))returnBOTTOM;else
returnDRAG;
}publicBitmap getCroppedBitmap() {
BitmapDrawable drawable=(BitmapDrawable) getDrawable();
//这里的x和y也不知道为什么是这样写的float x = leftTop.x - center.x + (drawable.getBitmap().getWidth() / 2);float y = leftTop.y - center.y + (drawable.getBitmap().getHeight() / 2);
Bitmap cropped= Bitmap.createBitmap(drawable.getBitmap(), (int) x,
(int) y, (int) rightBottom.x - (int) leftTop.x,
(int) rightBottom.y - (int) leftTop.y);
ByteArrayOutputStream stream= newByteArrayOutputStream();
cropped.compress(Bitmap.CompressFormat.PNG,100, stream);returncropped;
}
}