我想从图库或相机中裁剪图像,并获得原始图像的裁剪坐标X和Y,而不是裁剪的。
我需要这些坐标才能在POST请求中使用它们,在那里我发送原始图像和裁剪的坐标。
我该怎么做?
答案 0 :(得分:0)
您需要使用DragRectView类来读取所有Touch事件和Motion事件,然后在Canvas中绘制一个矩形。使用onTouchEvent函数,您可以从X和Y值获得坐标。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class DragRectView extends View {
private Paint mRectPaint;
private int mStartX = 0;
private int mStartY = 0;
private int mEndX = 0;
private int mEndY = 0;
private boolean mDrawRect = false;
private TextPaint mTextPaint = null;
private OnUpCallback mCallback = null;
public interface OnUpCallback {
void onRectFinished(Rect rect);
}
public DragRectView(final Context context) {
super(context);
init();
}
public DragRectView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}
public DragRectView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
init();
}
public void setOnUpCallback(OnUpCallback callback) {
mCallback = callback;
}
private void init() {
mRectPaint = new Paint();
mRectPaint.setColor(getContext().getResources().getColor(android.R.color.holo_gr een_light));
mRectPaint.setStyle(Paint.Style.STROKE);
mRectPaint.setStrokeWidth(5);
mTextPaint = new TextPaint();
mTextPaint.setColor(getContext().getResources().getColor(android.R.color.holo_gr een_light));
mTextPaint.setTextSize(20);
}
@Override
public boolean onTouchEvent(final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDrawRect = false;
mStartX = (int) event.getX();
mStartY = (int) event.getY();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
final int x = (int) event.getX();
final int y = (int) event.getY();
if (!mDrawRect || Math.abs(x - mEndX) > 5 || Math.abs(y - mEndY) > 5) {
mEndX = x;
mEndY = y;
invalidate();
}
mDrawRect = true;
break;
case MotionEvent.ACTION_UP:
if (mCallback != null) {
mCallback.onRectFinished(new Rect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
Math.max(mEndX, mStartX), Math.max(mEndY, mStartX)));
}
invalidate();
break;
default:
break;
}
return true;
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
if (mDrawRect) {
canvas.drawRect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
Math.max(mEndX, mStartX), Math.max(mEndY, mStartY), mRectPaint);
canvas.drawText(" (" + Math.abs(mStartX - mEndX) + ", " + Math.abs(mStartY - mEndY) + ")",
Math.max(mEndX, mStartX), Math.max(mEndY, mStartY), mTextPaint);
}
}
}
这将是主要方法:
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
Bitmap Imag;
ImageView view;
TextView text;
int picw, pich;
int pix[];
int x1, x2, y1, y2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DragRectView view2 = (DragRectView) findViewById(R.id.dragRect);
Imag = BitmapFactory.decodeResource(getResources(), R.drawable.rgb);
picw = Imag.getWidth();
pich = Imag.getHeight();
view = (ImageView) findViewById(R.id.image);
view.setImageBitmap(Imag);
text = (TextView) findViewById(R.id.tex);
pix = new int[picw * pich];
if (null != view2) {
view2.setOnUpCallback(new DragRectView.OnUpCallback() {
@Override
public void onRectFinished(final Rect rect) {
x1 = rect.left;
y1 = rect.top;
x2 = rect.right;
y2 = rect.bottom;
Toast.makeText(getApplicationContext(), "Coordenadas (" + x1 + ", " + y1 + ", " + x2 + ", " + y2 + ")", Toast.LENGTH_LONG).show();
}
});
}
}
}
答案 1 :(得分:0)
来自lockdawn的回答为我工作,但我必须纠正这条线......
Math.max(mEndX, mStartX), Math.max(mEndY, mStartX)));
我认为该行应该是:
Math.max(mEndX, mStartX), Math.max(mEndY, mStartY)));
答案 2 :(得分:0)
File tempDir = Environment.getExternalStorageDirectory();
tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
tempDir.mkdir();
File tempFile = null;
try {
tempFile = File.createTempFile("Trendii", ".png", tempDir);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
byte[] bitmapData = bytes.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(bitmapData);
fos.flush();
fos.close();
float height = 0, width = 0;
if (bitmap != null) {
height = bitmap.getHeight();
width = bitmap.getWidth();
}
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
Crop.of(Uri.fromFile(tempFile), destination, inspirationId, height, width).asSquare().start(InspirationDetailsActivity.this);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
showSettingsDialog();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
token.continuePermissionRequest();
}
}).check();
} else {
File tempDir = Environment.getExternalStorageDirectory();
tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
tempDir.mkdir();
File tempFile = null;
try {
tempFile = File.createTempFile("Trendii", ".png", tempDir);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
byte[] bitmapData = bytes.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(bitmapData);
fos.flush();
fos.close();
float height = 0, width = 0;
if (bitmap != null) {
height = bitmap.getHeight();
width = bitmap.getWidth();
}
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
Crop.of(Uri.fromFile(tempFile), destination, inspirationId, height, width).asSquare().start(this);
} catch (IOException e) {
e.printStackTrace();
}