public
class
CropImage
extends
ImageView {
private
static
final
String TAG =
"Crop_Image"
;
float
sx, ex, sy, ey;
static
int
DEP =
50
;
ContentResolver mContentResolver;
ImageCropActivity cnxt;
Bitmap bitmap;
float
mWidth , bitWidth;
float
mHeight , bitHeight;
Paint pnt;
Bitmap hBmp;
Bitmap wBmp;
Bitmap LTP,RTP,LBP,RBP;
Bitmap tmp;
private
String outFilePath = FileUtil.getStrFilePath();
public
CropImage(Context context, AttributeSet attr) {
super
(context, attr);
String ver = Build.VERSION.RELEASE;
if
(Integer.valueOf(ver.charAt(
0
)) <
3
|| ver.substring(
0
,
2
).equals(
"3.1"
)){
}
else
{
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point point =
new
Point();
display.getSize(point);
mWidth = point.x;
mHeight = point.y;
}
Log.e(TAG, outFilePath);
cnxt = (ImageCropActivity) context;
BitmapFactory.Options resizeOpts =
new
Options();
resizeOpts.inSampleSize =
2
;
try
{
bitmap = BitmapFactory.decodeStream(
new
FileInputStream(outFilePath),
null
, resizeOpts);
}
catch
(Exception e) {
e.printStackTrace();
}
bitmap = BitmapUtil.resizeBitmap(bitmap, (
int
)mWidth);
bitWidth = bitmap.getWidth();
bitHeight = bitmap.getHeight();
Log.e(TAG,
""
+ bitHeight * bitWidth);
LTP = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.circle);
LBP = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.circle);
RTP = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.circle);
RBP = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.circle);
if
(bitWidth > bitHeight){
sx = mWidth *
0
;
ex = bitWidth;
sy = mHeight/
7
;
ey = bitHeight + mHeight/
7
;
}
else
{
sx = (mWidth - bitWidth)/
2
;
ex = (mWidth - bitWidth)/
2
+ bitWidth;
sy = mHeight/
20
;
ey = bitHeight + mHeight/
20
;
}
Log.d(
"크기"
,
"mwidth :"
+ mWidth +
" mHeight : "
+ mHeight +
"sx : "
+ sx +
" ex : "
+ ex +
" sy : "
+ sy +
" ey :"
+ ey);
pnt =
new
Paint();
pnt.setColor(Color.WHITE);
pnt.setStrokeWidth(
3
);
}
public
void
onDraw(Canvas canvas) {
canvas.drawBitmap(bitmap, mHeight-bitHeight/
2
, mWidth-bitWidth/
2
,
null
);
if
(bitWidth > bitHeight){
canvas.drawBitmap(bitmap,
0
, mHeight/
7
,
null
);
}
else
{
canvas.drawBitmap(bitmap, ((mWidth - bitWidth)/
2
) , mHeight/
20
,
null
);
}
canvas.drawLine(sx, sy, ex, sy, pnt);
canvas.drawLine(ex, sy, ex, ey, pnt);
canvas.drawLine(sx, sy, sx, ey, pnt);
canvas.drawLine(sx, ey, ex, ey, pnt);
canvas.drawBitmap(LTP, sx-
30
, sy-
30
,
null
);
canvas.drawBitmap(RTP, ex-
30
, sy-
30
,
null
);
canvas.drawBitmap(LBP, sx-
30
, ey-
30
,
null
);
canvas.drawBitmap(RBP, ex-
30
, ey-
30
,
null
);
}
float
dx =
0
, dy =
0
;
float
oldx, oldy;
boolean
bsx, bsy, bex, bey;
boolean
bMove =
false
;
public
boolean
onTouchEvent(MotionEvent e) {
int
x = (
int
) e.getX();
int
y = (
int
) e.getY();
if
(e.getAction() == MotionEvent.ACTION_DOWN) {
oldx = x;
oldy = y;
if
((x > sx - DEP) && (x < sx + DEP))
bsx =
true
;
else
if
((x > ex - DEP) && (x < ex + DEP))
bex =
true
;
if
((y > sy - DEP) && (y < sy + DEP))
bsy =
true
;
else
if
((y > ey - DEP) && (y < ey + DEP))
bey =
true
;
if
((bsx || bex || bsy || bey))
bMove =
false
;
else
if
(((x > sx + DEP) && (x < ex - DEP))
&& ((y > sy + DEP) && (y < ey - DEP)))
bMove =
true
;
return
true
;
}
if
(e.getAction() == MotionEvent.ACTION_MOVE) {
if
(bsx)
sx = x;
if
(bex)
ex = x;
if
(bsy)
sy = y;
if
(bey)
ey = y;
if
(ex <= sx + DEP) {
ex = sx + DEP;
return
true
;
}
if
(ey <= sy + DEP) {
ey = sy + DEP;
return
true
;
}
if
(bMove) {
dx = oldx - x;
dy = oldy - y;
sx -= dx;
ex -= dx;
sy -= dy;
ey -= dy;
if
(bitWidth>bitHeight? sx <=
0
: sx <= (mWidth - bitWidth)/
2
)
sx = bitWidth > bitHeight ?
0
: (mWidth - bitWidth)/
2
;
if
(bitWidth>bitHeight ? ex >= mWidth : ex >= (mWidth - bitWidth)/
2
+ bitWidth)
ex = bitWidth > bitHeight ? mWidth -
1
: (mWidth - bitWidth)/
2
+ bitWidth;
if
(bitWidth>bitHeight ?sy <= mHeight/
7
: sy <= mHeight/
20
)
sy = bitWidth>bitHeight ? mHeight/
7
: mHeight/
20
;
if
(bitWidth>bitHeight ? ey >= bitHeight + mHeight/
7
: ey >= bitHeight + mHeight/
20
)
ey = bitWidth>bitHeight ? bitHeight + mHeight/
7
-
1
: bitHeight + mHeight/
20
-
1
;
}
invalidate();
oldx = x;
oldy = y;
return
true
;
}
if
(e.getAction() == MotionEvent.ACTION_UP) {
bsx = bex = bsy = bey = bMove =
false
;
return
true
;
}
return
false
;
}
public
void
save() {
try
{
if
(bitWidth > bitHeight){
tmp = Bitmap.createBitmap(bitmap, (
int
) (sx *
0
), (
int
) (sy-mHeight/
7
),
(
int
) (ex - sx), (
int
) (ey - sy));
}
else
{
tmp = Bitmap.createBitmap(bitmap, (
int
) (sx - (mWidth - bitWidth)/
2
), (
int
) (sy-mHeight/
20
),
(
int
) (ex - sx), (
int
) (ey - sy));
}
}
catch
(IllegalArgumentException e) {
DefaultErrorHandler.handleError(getContext(), e ,
"자르는 선 안쪽으로 배치시켜주세요."
);
}
byte
[] byteArray = bitmapToByteArray(tmp);
File file =
new
File(outFilePath);
try
{
FileOutputStream fos =
new
FileOutputStream(file);
fos.write(byteArray);
fos.flush();
fos.close();
}
catch
(Exception e) {
Toast.makeText(cnxt,
"파일 저장 중 에러 발생 : "
+ e.getMessage(),
0
).show();
return
;
}
}
public
byte
[] bitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream stream =
new
ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG,
100
, stream);
byte
[] byteArray = stream.toByteArray();
return
byteArray;
}
}