之前做android开发,遇到从照相机、图库获取照片的问题,网上分享的有很多,但是都是比较杂乱的,而且很多的程序都是有问题的,在此针对手机图像的几种操作做一下总结:
1.相机获取图像:
2.从相机图库中获取照片
//*******调用系统相机拍照,并将图像存储到本地的ou给出的URIt路径下
private void getPhotoFromCamera(){
Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(!ImageTools.checkSDCardAvailable()){
Toast.makeText(getApplicationContext(), "请检查您的存储卡...", Toast.LENGTH_SHORT).show();
return;
}
fileName = new SimpleDateFormat("yyyyMMddHHmmss")
.format(new Date()) + AppContext.getUSER_NUM() + ".jpg";// 照片命名
File out = new File(strImgPath);
if (!out.exists()) {
out.mkdirs();
}
out = new File(strImgPath, fileName);
Uri uri = Uri.fromFile(out);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(imageCaptureIntent, ResultCodeInfo.DNIF_CAMERA_PHOTO);
}
//***********从本地路径out中获取图像显示在ImageView中
Bitmap bitmap = BitmapFactory.decodeFile(strImgPath + fileName);
imageView..setImageBitmap(bitmap);
//***********调用图像图库
Intent choose_intent = new Intent();
choose_intent.setType("image/*");
choose_intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(choose_intent, ResultCodeInfo.DNIF_PICK_PHOTO);
//**********取出图库中的照片,并且重名到系统指定路径的文件夹下
try {
ContentResolver resolver = getContentResolver();
Uri pick_photo_uri = data.getData();
Bitmap bm = MediaStore.Images.Media.getBitmap(resolver, pick_photo_uri);
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(pick_photo_uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String oldPath = cursor.getString(column_index);
fileName = new SimpleDateFormat("yyyyMMddHHmmss")
.format(new Date()) + AppContext.getUSER_NUM() + ".jpg";
//重命名从相册中的图片,存到/door_num文件夹中
File pick_photo = new File(oldPath);
pick_photo.renameTo(new File(strImgPath,fileName));
ImageTools.savePhotoToSDCard(bm, strImgPath, fileName);
//***************点击ImageView显示该图片,并且实现了两个手指点击图像放大缩小,并且图像缩小的比例宽不可以小于屏幕宽度
//调用浏览编辑图像的activity
Intent browser_photo_intent = new Intent();
browser_photo_intent.putExtra("image_uri", strImgPath + fileName);
browser_photo_intent.setClass(DoorNumInfoDetail.this, BrowserPhotoActivity.class);
startActivity(browser_photo_intent);
*****************************BrowserPhotoActivity************************************************************
public class BrowserPhotoActivity extends Activity implements OnTouchListener{
private String imageUri;
private int window_width, window_height;// 控件宽度
private DragImageView dragImageView;// 自定义控件
private int state_height;// 状态栏的高度
private ViewTreeObserver viewTreeObserver;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.browser_photo);
//获取图片可见区域的高度
WindowManager manager = getWindowManager();
window_width = manager.getDefaultDisplay().getWidth();
window_height = manager.getDefaultDisplay().getHeight();
dragImageView = (DragImageView) findViewById(R.id.div_main);
imageUri = getIntent().getExtras().getString("image_uri");
if(ImageTools.Exist(imageUri)){
Bitmap bmp = BitmapFactory.decodeFile(imageUri);
Bitmap newBitmap = getBitmap(bmp, window_width, window_height);
dragImageView.setImageBitmap(newBitmap);
dragImageView.setmActivity(this);
// 测量状态栏高度
viewTreeObserver = dragImageView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (state_height == 0) {
// 获取状况栏高度
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
state_height = frame.top;
dragImageView.setScreen_H(window_height-state_height);
dragImageView.setScreen_W(window_width);
}
}
});
}else{
Toast.makeText(this, "照片打开错误...", Toast.LENGTH_SHORT).show();
this.finish();
return;
}
}
/***
* 等比例压缩图片
* */
public static Bitmap getBitmap(Bitmap bitmap, int screenWidth,
int screenHight) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scale = (float) screenWidth / w;
float scale2 = (float) screenHight / h;
matrix.postScale(scale, scale);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
***********************DragImageView***************************************************
public class DragImageView extends ImageView {
private Activity mActivity;
private int screen_W, screen_H;// 可见屏幕的宽高度
private int bitmap_W, bitmap_H;// 当前图片宽高
private int MAX_W, MAX_H, MIN_W, MIN_H;// 极限值
private int current_Top, current_Right, current_Bottom, current_Left;// 当前图片上下左右坐标
private int start_Top = -1, start_Right = -1, start_Bottom = -1,start_Left = -1;// 初始化默认位置.
private int start_x, start_y, current_x, current_y;// 触摸位置
private float beforeLenght, afterLenght;// 两触点距离
private float scale_temp;// 缩放比例
private enum MODE {
NONE, DRAG, ZOOM
};
private MODE mode = MODE.NONE;// 默认模式
private boolean isControl_V = false;// 垂直监控
private boolean isControl_H = false;// 水平监控
private ScaleAnimation scaleAnimation;// 缩放动画
private boolean isScaleAnim = false;// 缩放动画
private MyAsyncTask myAsyncTask;// 异步动画
public DragImageView(Context context) {
super(context);
}
public void setmActivity(Activity mActivity) {
this.mActivity = mActivity;
}
public void setScreen_W(int screen_W) {
this.screen_W = screen_W;
}
public void setScreen_H(int screen_H) {
this.screen_H = screen_H;
}
public DragImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
/** 获取图片宽高 **/
bitmap_W = bm.getWidth();
bitmap_H = bm.getHeight();
MAX_W = bitmap_W * 3;
MAX_H = bitmap_H * 3;
MIN_W = bitmap_W / 2;
MIN_H = bitmap_H / 2;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (start_Top == -1) {
start_Top = top;
start_Left = left;
start_Bottom = bottom;
start_Right = right;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
/** 处理单点、多点触摸 **/
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
onTouchDown(event);
break;
// 多点触摸
case MotionEvent.ACTION_POINTER_DOWN:
onPointerDown(event);
break;
case MotionEvent.ACTION_MOVE:
onTouchMove(event);
break;
case MotionEvent.ACTION_UP:
mode = MODE.NONE;
break;
// 多点松开
case MotionEvent.ACTION_POINTER_UP:
mode = MODE.NONE;
/** 执行缩放还原 **/
if (isScaleAnim) {
doScaleAnim();
}
break;
}
return true;
}
void onTouchDown(MotionEvent event) {
mode = MODE.DRAG;
current_x = (int) event.getRawX();
current_y = (int) event.getRawY();
start_x = (int) event.getX();
start_y = current_y - this.getTop();
}
/** 两个手指 只能放大缩小 **/
void onPointerDown(MotionEvent event) {
if (event.getPointerCount() == 2) {
mode = MODE.ZOOM;
beforeLenght = getDistance(event);// 获取两点的距离
}
}
/** 移动的处理 **/
void onTouchMove(MotionEvent event) {
int left = 0, top = 0, right = 0, bottom = 0;
if (mode == MODE.DRAG) {
left = current_x - start_x;
right = current_x + this.getWidth() - start_x;
top = current_y - start_y;
bottom = current_y - start_y + this.getHeight();
if (isControl_H) {
if (left >= 0) {
left = 0;
right = this.getWidth();
}
if (right <= screen_W) {
left = screen_W - this.getWidth();
right = screen_W;
}
} else {
left = this.getLeft();
right = this.getRight();
}
if (isControl_V) {
if (top >= 0) {
top = 0;
bottom = this.getHeight();
}
if (bottom <= screen_H) {
top = screen_H - this.getHeight();
bottom = screen_H;
}
} else {
top = this.getTop();
bottom = this.getBottom();
}
if (isControl_H || isControl_V)
this.setPosition(left, top, right, bottom);
current_x = (int) event.getRawX();
current_y = (int) event.getRawY();
}
else if (mode == MODE.ZOOM) {
afterLenght = getDistance(event);
float gapLenght = afterLenght - beforeLenght;
if (Math.abs(gapLenght) > 5f) {
scale_temp = afterLenght / beforeLenght;
this.setScale(scale_temp);
beforeLenght = afterLenght;
}
}
}
/** 获取两点的距离 **/
float getDistance(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** 实现处理拖动 **/
private void setPosition(int left, int top, int right, int bottom) {
this.layout(left, top, right, bottom);
}
/** 处理缩放 **/
void setScale(float scale) {
int disX = (int) (this.getWidth() * Math.abs(1 - scale)) / 4;// 获取缩放水平距离
int disY = (int) (this.getHeight() * Math.abs(1 - scale)) / 4;// 获取缩放垂直距离
if (scale > 1 && this.getWidth() <= MAX_W) {
current_Left = this.getLeft() - disX;
current_Top = this.getTop() - disY;
current_Right = this.getRight() + disX;
current_Bottom = this.getBottom() + disY;
this.setFrame(current_Left, current_Top, current_Right,
current_Bottom);
if (current_Top <= 0 && current_Bottom >= screen_H) {
isControl_V = true;
} else {
isControl_V = false;
}
if (current_Left <= 0 && current_Right >= screen_W) {
isControl_H = true;
} else {
isControl_H = false;
}
}
// 缩小
else if (scale < 1 && this.getWidth() >= MIN_W) {
current_Left = this.getLeft() + disX;
current_Top = this.getTop() + disY;
current_Right = this.getRight() - disX;
current_Bottom = this.getBottom() - disY;
if (isControl_V && current_Top > 0) {
current_Top = 0;
current_Bottom = this.getBottom() - 2 * disY;
if (current_Bottom < screen_H) {
current_Bottom = screen_H;
isControl_V = false;
}
}
if (isControl_V && current_Bottom < screen_H) {
current_Bottom = screen_H;
current_Top = this.getTop() + 2 * disY;
if (current_Top > 0) {
current_Top = 0;
isControl_V = false;
}
}
if (isControl_H && current_Left >= 0) {
current_Left = 0;
current_Right = this.getRight() - 2 * disX;
if (current_Right <= screen_W) {
current_Right = screen_W;
isControl_H = false;// 关闭
}
}
if (isControl_H && current_Right <= screen_W) {
current_Right = screen_W;
current_Left = this.getLeft() + 2 * disX;
if (current_Left >= 0) {
current_Left = 0;
isControl_H = false;// 关闭
}
}
if (isControl_H || isControl_V) {
this.setFrame(current_Left, current_Top, current_Right,
current_Bottom);
} else {
this.setFrame(current_Left, current_Top, current_Right,
current_Bottom);
isScaleAnim = true;// 开启缩放动画
}
}
}
public void doScaleAnim() {
myAsyncTask = new MyAsyncTask(screen_W, this.getWidth(),
this.getHeight());
myAsyncTask.setLTRB(this.getLeft(), this.getTop(), this.getRight(),
this.getBottom());
myAsyncTask.execute();
isScaleAnim = false;// 关闭动画
}
class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
private int screen_W, current_Width, current_Height;
private int left, top, right, bottom;
private float scale_WH;// 宽高的比例
public void setLTRB(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
private float STEP = 8f;// 步伐
private float step_H, step_V;// 水平步伐,垂直步伐
public MyAsyncTask(int screen_W, int current_Width, int current_Height) {
super();
this.screen_W = screen_W;
this.current_Width = current_Width;
this.current_Height = current_Height;
scale_WH = (float) current_Height / current_Width;
step_H = STEP;
step_V = scale_WH * STEP;
}
@Override
protected Void doInBackground(Void... params) {
while (current_Width <= screen_W) {
left -= step_H;
top -= step_V;
right += step_H;
bottom += step_V;
current_Width += 2 * step_H;
left = Math.max(left, start_Left);
top = Math.max(top, start_Top);
right = Math.min(right, start_Right);
bottom = Math.min(bottom, start_Bottom);
Log.e("jj", "top="+top+",bottom="+bottom+",left="+left+",right="+right);
onProgressUpdate(new Integer[] { left, top, right, bottom });
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(final Integer... values) {
super.onProgressUpdate(values);
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
setFrame(values[0], values[1], values[2], values[3]);
}
});
}
}
}