在我的项目中,我可以选择图片或用相机拍照。
当我想将它加载到图像视图中时,我的图片会旋转。
我怎么能避免旋转。我的图片视图ID服装图片视图,它是DraggableImageView
。
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
btnTakePhoto = (ImageButton) findViewById(R.id.btn_take_photo);
btnFromDir = (ImageButton) findViewById(R.id.btn_from_dir);
btnTakePhoto.setOnClickListener(this);
btnFromDir.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btn_take_photo) {
MenuActivityHelper.clearCameraTempFile();
MenuActivityHelper.callCameraApp(this);
} else if (id == R.id.btn_from_dir) {
MenuActivityHelper.clearCameraTempFile();
MenuActivityHelper.callGalleryApp(this);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case vlCameraConstant.CAMERA_REQUEST:
try {
if (resultCode == RESULT_CANCELED) return;
Uri cameraTempUri = MenuActivityHelper.getCameraTempFile(this);
if (cameraTempUri != null) {
Intent editIntent = new Intent(this, EditActivity.class);
editIntent.setData(cameraTempUri);
startActivity(editIntent);
} else {
throw new Exception();
}
} catch (Exception e) {
Log.v(TAG, "Can't create file to take picture!");
}
break;
case vlCameraConstant.GALLERY_REQUEST:
if (data != null && data.getData() != null) {
data.setClass(this, EditActivity.class);
startActivity(data);
}
break;
default:
// Do nothing
}
}
我的自定义imageview:
public class DraggableImageView extends ImageView {
// some private variable use for detect multi touch
public enum EDITMODE {
NONE, DRAG, ZOOM, ROTATE
}
private static final String TAG = "Draggable Bitmap";
private boolean mDrawOpacityBackground = false;
private Paint mPaint = new Paint();
private DraggableBitmap mActiveBitmap = null;
private RectF mInnerImageBounds = null;
private Stack<BitmapOperationMap> mOperationStack = new Stack<BitmapOperationMap>();
// list of stamp bitmaps
private List<DraggableBitmap> mOverlayBitmaps;
// constructors
public DraggableImageView(Context context) {
super(context);
initMembers();
this.setOnTouchListener(touchListener);
}
public DraggableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
initMembers();
this.setOnTouchListener(touchListener);
}
private void initMembers() {
mOverlayBitmaps = new ArrayList<DraggableBitmap>();
}
// listeners
private OnTouchListener touchListener = new OnTouchListener() {
// to get mode [drag, zoom, rotate]
private EDITMODE mEditMode = EDITMODE.NONE;
private float[] mLastEvent;
private PointF mStart = new PointF();
private PointF mMid = new PointF();
private float mOldDistance;
private float mNewRotation = 0f;
private float mDist = 0f;
// this variable use to deal with android odd touch behavior (MOVE -> UP
// -> MOVE -> UP)
private boolean touchMoveEndChecker = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
// switch finger events
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case (MotionEvent.ACTION_DOWN):
touchMoveEndChecker = true;
mDrawOpacityBackground = true;
int activebmpIdx = getActiveBitmap(event.getX(), event.getY());
if (activebmpIdx != -1) {
mActiveBitmap = mOverlayBitmaps.get(activebmpIdx);
rearrangeOverlayList();
}
else {
mActiveBitmap = null;
break;
}
mLastEvent = null;
mEditMode = EDITMODE.DRAG;
mStart.set(event.getX(), event.getY());
if (mActiveBitmap != null) {
mActiveBitmap.setSavedMatrix(mActiveBitmap.getCurrentMatrix());
}
break;
case (MotionEvent.ACTION_POINTER_DOWN):
touchMoveEndChecker = false;
mDrawOpacityBackground = true;
if (mActiveBitmap != null) {
mOldDistance = spacing(event);
if (mOldDistance > 10f) {
mActiveBitmap.setSavedMatrix(mActiveBitmap.getCurrentMatrix());
midPoint(mMid, event);
mEditMode = EDITMODE.ZOOM;
}
mLastEvent = new float[4];
mLastEvent[0] = event.getX(0);
mLastEvent[1] = event.getX(1);
mLastEvent[2] = event.getY(0);
mLastEvent[3] = event.getY(1);
mDist = rotation(event);
}
break;
case (MotionEvent.ACTION_POINTER_UP):
mEditMode = EDITMODE.NONE;
break;
case (MotionEvent.ACTION_MOVE):
touchMoveEndChecker = false;
mDrawOpacityBackground = true;
if (mActiveBitmap != null) {
if (mEditMode == EDITMODE.DRAG) {
mActiveBitmap.setCurrentMatrix(mActiveBitmap.getSavedMatrix());
mActiveBitmap.getCurrentMatrix().postTranslate(event.getX() - mStart.x,
event.getY() - mStart.y);
} else if (mEditMode == EDITMODE.ZOOM && event.getPointerCount() == 2) {
float newDistance = spacing(event);
mActiveBitmap.setCurrentMatrix(mActiveBitmap.getSavedMatrix());
if (newDistance > 10f) {
float scale = newDistance / mOldDistance;
mActiveBitmap.getCurrentMatrix()
.postScale(scale, scale, mMid.x, mMid.y);
}
if (mLastEvent != null) {
mNewRotation = rotation(event);
float r = mNewRotation - mDist;
RectF rec = new RectF(0, 0, mActiveBitmap.mBitmap.getWidth(),
mActiveBitmap.mBitmap.getHeight());
mActiveBitmap.getCurrentMatrix().mapRect(rec);
mActiveBitmap.getCurrentMatrix().postRotate(r,
rec.left + rec.width() / 2, rec.top + rec.height() / 2);
}
}
}
case (MotionEvent.ACTION_UP):
if (touchMoveEndChecker) { // means 2 continuous ACTION_UP, or
// real finger up after moving
mDrawOpacityBackground = false;
if (mActiveBitmap != null) {
// push a map to bitmap and clone of current matrix
mOperationStack
.push(new BitmapOperationMap(mActiveBitmap, new Matrix(
mActiveBitmap.getCurrentMatrix()),
BitmapOperationMap.OPERATION.ADD));
mActiveBitmap.deActivate();
}
}
touchMoveEndChecker = true;
default:
break;
}
invalidate();
return true;
}
};
public void addOverlayBitmap(DraggableBitmap dBitmap, float scale) {
Matrix marginMtx = new Matrix();
marginMtx.postTranslate(mInnerImageBounds.left, mInnerImageBounds.top);
dBitmap.setMarginMatrix(marginMtx);
Matrix curMtx = new Matrix();
curMtx.postConcat(marginMtx);
dBitmap.setCurrentMatrix(curMtx);
mOperationStack
.push(new BitmapOperationMap(dBitmap, null, BitmapOperationMap.OPERATION.NEW));
mOperationStack.push(new BitmapOperationMap(dBitmap, dBitmap.getCurrentMatrix(),
BitmapOperationMap.OPERATION.ADD));
mOverlayBitmaps.add(dBitmap);
}
private int getActiveBitmap(float event_x, float event_y) {
int size = mOverlayBitmaps.size();
int retidx = -1;
DraggableBitmap retBmp = null;
// search for all bitmap to find closest to finger
for (int i = 0; i < size; i++) {
DraggableBitmap dBmp = mOverlayBitmaps.get(i);
dBmp.deActivate();
float bmp_x = 0;
float bmp_y = 0;
RectF r = new RectF(0, 0, dBmp.mBitmap.getWidth(), dBmp.mBitmap.getHeight());
Matrix mtx = dBmp.getCurrentMatrix() == null ? dBmp.getMarginMatrix() : dBmp
.getCurrentMatrix();
mtx.mapRect(r);
bmp_x = r.left;
bmp_y = r.top;
if (event_x >= bmp_x && event_x < (bmp_x + r.width()) && event_y >= bmp_y
&& event_y < (bmp_y + r.height())) {
retBmp = dBmp;
retidx = i;
}
}
if (retBmp != null) {
if (!retBmp.isTouched()) {
retBmp.setTouched(true);
}
retBmp.activate();
}
return retidx;
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
private float rotation(MotionEvent event) {
double delta_x = (event.getX(0) - event.getX(1));
double delta_y = (event.getY(0) - event.getY(1));
double rad = Math.atan2(delta_y, delta_x);
return (float) Math.toDegrees(rad);
}
public List<DraggableBitmap> getOverlayList() {
return mOverlayBitmaps;
}
public void undo() {
if (!mOperationStack.empty()) {
BitmapOperationMap prev = mOperationStack.pop();
if (!mOperationStack.empty()) { // current stack is final operation
prev = mOperationStack.peek();
}
DraggableBitmap bmp = prev.getDraggableBitmap();
Matrix mtx = prev.getOperationMatrix();
switch (prev.getOption()) {
case NEW: // if action is create new, then delete
mOverlayBitmaps.remove(bmp);
break;
case ADD:
bmp.setCurrentMatrix(mtx);
break;
case DELETE: // not implement yet
break;
default:
break;
}
}
}
@Override
protected void onDraw(Canvas canvas) { // [TODO] khi xoay man hinh error
super.onDraw(canvas);
RectF bitmapRect = getInnerBitmapSize();
if (bitmapRect == null) return;
mInnerImageBounds = bitmapRect;
canvas.clipRect(bitmapRect);
// loop to draw all bitmap
Enumeration<DraggableBitmap> e = Collections.enumeration(mOverlayBitmaps);
while (e.hasMoreElements()) {
DraggableBitmap dBmp = (DraggableBitmap) e.nextElement();
if (true) {
if (dBmp.getCurrentMatrix() != null) {
canvas.drawBitmap(dBmp.mBitmap, dBmp.getCurrentMatrix(), null);
RectF r = getStampBounding(dBmp);
if (mDrawOpacityBackground && dBmp == mActiveBitmap) {
mPaint.setColor(0x00000000);
mPaint.setStyle(Style.FILL);
mPaint.setAlpha(20);
canvas.drawRect(r, mPaint);
}
}
}
}
}
public RectF getInnerBitmapSize() {
RectF bitmapRect = new RectF();
if (this.getDrawable() == null) return null;
bitmapRect.right = this.getDrawable().getIntrinsicWidth();
bitmapRect.bottom = this.getDrawable().getIntrinsicHeight();
Matrix m = this.getImageMatrix();
m.mapRect(bitmapRect);
return bitmapRect;
}
private RectF getStampBounding(DraggableBitmap bmp) {
if (bmp.mBitmap == null) return null;
RectF r = new RectF(0, 0, bmp.mBitmap.getWidth(), bmp.mBitmap.getHeight());
bmp.getCurrentMatrix().mapRect(r);
return r;
}
public void deleteActiveBitmap() {
if (mActiveBitmap == null) return;
mOverlayBitmaps.remove(mActiveBitmap);
}
public void flipActiveBitmap() {
try {
Matrix flipHorizontalMtx = new Matrix();
flipHorizontalMtx.setScale(-1, 1);
flipHorizontalMtx.postTranslate((float) (mActiveBitmap.mBitmap.getWidth()), (float) 0);
Matrix mtx = mActiveBitmap.getCurrentMatrix();
mtx.preConcat(flipHorizontalMtx);
mActiveBitmap.setCurrentMatrix(mtx);
} catch (NullPointerException e) {
Log.v(TAG, "active bitmap is null");
} catch (Exception e) {
Log.v(TAG, "error ocurred");
}
}
public void rearrangeOverlayList() {
int idx = mOverlayBitmaps.indexOf(mActiveBitmap);
mOverlayBitmaps.add(mActiveBitmap);
mOverlayBitmaps.remove(idx);
}
}
和我的编辑活动显示我的图片已旋转:
public class EditActivity extends BaseActivity implements View.OnClickListener {
private static final String TAG = "EditActivity";
private DraggableImageView mImageView;
private Bitmap mRawBitmap;
private int mCurrentTextColor;
ImageButton mButtonAdd;
ImageButton mButtonDelete;
ImageButton mButtonAddText;
ImageButton mButtonFlip;
ImageButton mButtonFinish;
ImageButton mButtonBack;
EditActivityHelper mActivityHelper;
/** listerner **/
EditActivityHelper.popupTextEditListener addtextListener =
new EditActivityHelper.popupTextEditListener() {
@Override
public void onOkClick(String addText) {
if (addText.length() == 0) return;
mActivityHelper.addTextToBitmap(addText, getCurrentTextColor());
}
@Override
public void onCancelClick() {}
};
/*** default stuffs ***/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
mActivityHelper = new EditActivityHelper(this);
mImageView = (DraggableImageView) findViewById(R.id.edit_imageview);
mButtonAdd = (ImageButton) findViewById(R.id.btn_add);
mButtonDelete = (ImageButton) findViewById(R.id.btn_delete);
mButtonFlip = (ImageButton) findViewById(R.id.btn_flip);
mButtonFinish = (ImageButton) findViewById(R.id.btn_finish);
mButtonBack = (ImageButton) findViewById(R.id.btn_back);
mButtonAdd.setOnClickListener(this);
mButtonDelete.setOnClickListener(this);
mButtonFlip.setOnClickListener(this);
mButtonFinish.setOnClickListener(this);
mButtonBack.setOnClickListener(this);
Uri selectedImageUri;
final Intent intent = getIntent();
if (intent != null && intent.getData() != null) {
selectedImageUri = intent.getData();
mActivityHelper.displayPreviewImage(selectedImageUri, this);
}
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btn_add) {
Intent stampIntent = new Intent(this, StampChooseActivity.class);
startActivityForResult(stampIntent, vlCameraConstant.STAMP_REQUEST);
} else if (id == R.id.btn_flip) {
mActivityHelper.flipActiveBitmap();
} else if (id == R.id.btn_finish) {
Bitmap bmpToSave = mActivityHelper.saveCurrentBitmap();
this.getBaseApplication().setRawBitmap(bmpToSave);
Intent shareIntent = new Intent(this, ShareActivity.class);
startActivity(shareIntent);
} else if (id == R.id.btn_delete){
getImageView().deleteActiveBitmap();
getImageView().invalidate();
} else if (id == R.id.btn_back) {
finish();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case vlCameraConstant.STAMP_REQUEST:
try {
if (resultCode == vlCameraConstant.RESULT_CODE_ADDBITMAP) {
DraggableBitmap stamp = new DraggableBitmap((Bitmap) data.getExtras().get("data"));
getImageView().addOverlayBitmap(stamp, (float)1.0);
getImageView().invalidate();
} else if (resultCode == vlCameraConstant.RESULT_CODE_ADDTEXT) {
int color = (Integer) data.getExtras().get("color");
setCurrentTextColor(color);
mActivityHelper.popupTextEdit(addtextListener);
}
break;
} catch (Exception e) {
Log.v(TAG, "get extras error");
e.printStackTrace();
}
default:
break;
}
}
/*** get/set stuffs ***/
public DraggableImageView getImageView() {
return mImageView;
}
public Bitmap getRawBitmap() {
return mRawBitmap;
}
public void setRawBitmap(Bitmap mRawBitmap) {
this.mRawBitmap = mRawBitmap;
}
public int getCurrentTextColor() {
return mCurrentTextColor;
}
public void setCurrentTextColor(int mCurrentTextColor) {
this.mCurrentTextColor = mCurrentTextColor;
}
}
答案 0 :(得分:0)
你应该使用EXIF获取位图的方向然后旋转你的位图,在你的Imageview中设置旋转的位图, 这是代码;
additive_expression returns [value] @init{$value = 0;}
: multiplicative_expression ((PLUS_OPERATOR | MINUS_OPERATOR) multiplicative_expression)*
答案 1 :(得分:0)
我像这样添加你的代码:
我的代码:
public static Bitmap downSampleBitmap(Uri uri, Activity act, Boolean needRotate) {
DisplayMetrics displaymetrics = new DisplayMetrics();
act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
Resources r = act.getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
50, r.getDisplayMetrics()); // 50: magic num
int targetWidth = displaymetrics.heightPixels;
int targetHeight = displaymetrics.widthPixels - px;
Bitmap resizedBitmap = decodeSampledBitmap(uri, targetWidth, targetHeight, act);
Bitmap returnBitmap = null;
ExifInterface exif;
try {
exif = new ExifInterface(uri.toString());
int orient = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
rotateBitmap(returnBitmap,orient);
returnBitmap = returnBitmap == null ? resizedBitmap : returnBitmap;
} catch (IOException e) {
Log.v(TAG, "not found file at downsample");
e.printStackTrace();
}
return returnBitmap;
}