package
com.example.demoproject;
import
java.io.FileNotFoundException;
import
java.io.IOException;
import
java.io.InputStream;
import
android.annotation.SuppressLint;
import
android.app.Activity;
import
android.content.Context;
import
android.content.Intent;
import
android.database.Cursor;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.graphics.Rect;
import
android.media.ExifInterface;
import
android.net.Uri;
import
android.os.Bundle;
import
android.provider.MediaStore;
import
android.util.Log;
import
android.view.Display;
import
android.view.View;
import
android.view.WindowManager;
import
android.widget.ImageView;
public
class
ImageCropMainActivity
extends
Activity{
protected
ImageView bfImage;
protected
Bitmap bitmap;
protected
ExifInterface exif =
null
;
protected
InputStream stream =
null
;
protected
BitmapUtil bitmapUtil =
new
BitmapUtil();
protected
Util util =
new
Util();
private
static
final
int
PIC_CROP =
1
;
private
int
exifOrientation;
private
int
exifDegree;
private
Uri uriPath;
private
String path;
private
String strpath;
private
float
mWidth;
private
float
mHeight;
private
float
mXLeft, mXRight , mYTop, mYBottom;
private
static
int
mDep =
0
;
Paint mPaint;
Bitmap mLSizeBmp;
Bitmap mRSizeBmp;
@SuppressLint
(
"WrongCall"
)
@Override
protected
void
onCreate(Bundle savedInstanceState){
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_crop_main);
bfImage = (ImageView) findViewById(R.id.bfImage);
bfImage.setVisibility(View.VISIBLE);
Intent getImage =
this
.getIntent();
BitmapFactory.Options options =
new
BitmapFactory.Options();
strpath = getImage.getStringExtra(
"Path"
);
uriPath = util.getPath(strpath);
try
{
if
(bitmap !=
null
) {
bitmap.recycle();
}
stream = getContentResolver().openInputStream(uriPath);
bitmap = BitmapFactory.decodeStream(stream,
null
, options);
options.inSampleSize =
2
;
mWidth = options.outWidth;
mHeight = options.outHeight;
this
.bitmap = bitmapUtil.cropCenterBitmap(bitmap, (
int
)mWidth, (
int
)mHeight);
Cursor c = getContentResolver().query(uriPath,
null
,
null
,
null
,
null
);
c.moveToNext();
path = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
c.close();
exif =
new
ExifInterface(path);
exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
exifDegree = bitmapUtil.exifOrientationToDegrees(exifOrientation);
bitmap = bitmapUtil.rotate(bitmap, exifDegree);
bfImage.setImageBitmap(bitmap);
CustomView cv =
new
CustomView(
this
);
cv.onDraw(bitmap);
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException e){
e.printStackTrace();
}
finally
{
if
(stream !=
null
) {
try
{
stream.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
}
class
CustomView
extends
View {
public
CustomView(Context context){
super
(context);
}
protected
void
onDraw(Bitmap bitmap){
Canvas canvas =
new
Canvas(bitmap);
super
.onDraw(canvas);
mLSizeBmp = BitmapFactory.decodeResource(getResources(), R.drawable.lefttop);
mRSizeBmp = BitmapFactory.decodeResource(getResources(), R.drawable.righttop);
mPaint =
new
Paint();
mPaint.setColor(Color.MAGENTA);
mPaint.setStrokeWidth(
3
);
mXLeft = mWidth /
5
;
mXRight = mWidth *
4
/
5
;
mYTop = mHeight /
5
;
mYBottom = mHeight *
4
/
5
;
mDep = bfImage.getWidth()/
2
;
Log.d(
"Good"
,
"mXLeft : "
+ mXLeft +
" mYTop : "
+ mYTop +
" mXRight : "
+ mXRight +
"mYTop : "
+ mYTop +
"mPaint : "
+ mPaint +
"mDep : "
+ mDep);
canvas.drawBitmap(bitmap,
0
,
0
,
null
);
canvas.drawLine(mXLeft, mYTop, mXRight, mYTop, mPaint);
canvas.drawLine(mXRight, mYTop, mXRight, mYBottom, mPaint);
canvas.drawLine(mXLeft, mYTop, mXLeft, mYBottom, mPaint);
canvas.drawLine(mXLeft, mYBottom, mXRight, mYBottom, mPaint);
canvas.drawBitmap(mLSizeBmp, mXLeft - (mDep), mYTop- (mDep),
null
);
canvas.drawBitmap(mRSizeBmp, mXRight - (mDep), mYTop- (mDep),
null
);
canvas.drawBitmap(mRSizeBmp, mXLeft - (mDep), mYBottom- (mDep),
null
);
canvas.drawBitmap(mLSizeBmp, mXRight - (mDep), mYBottom- (mDep),
null
);
}
}
class
Util {
public
Uri getPath(String Path){
try
{
if
(Path ==
null
){
Path =
""
;
}
}
catch
(Exception e) {
}
return
Uri.parse(Path);
}
}
}