分享一个需要拍照的界面可以直接继承的Activity,附带详细注解
2015-04-24 17:08
218 查看
需要拍照的界面可以根据自己项目需求直接继承该Activity,附带详细注解,拍照剪切专题会进行详细讲解,大结局篇给出一个完整的总结,
public abstract class RequestWithTakePhotoActivity extends BaseActivity {
private static final String IMAGE_UNSPECIFIED = "image/*";
private static final String IMAGE_TMP = "photo_tmp.png";
private static final String IMAGE_TMP_2 = "photo_tmp2.png";
private static final int TAKE_PHOTO = 0;
private static final int PIC_PHOTO = 1;
// private static final int CROP_PHOTO = 2;
private static final int CROP_BIG_PICTURE = 3;
private boolean enableCrop = true;
private static final int MAX_LINE = 480;
private Uri imageUri;
protected PopDialog dialog = null;
private ArrayList<Integer> save_images_arr_id = new ArrayList<Integer>();
private ArrayList<String> save_images_arr_paths = new ArrayList<String>();
public final static int GET_PHOTO_RESULT_ID = 6;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File outFile = new File(getExternalCacheDir() + "/" + IMAGE_TMP);
imageUri = Uri.fromFile(outFile);
//测试后会发现并没有进入到onCreate方法//不对,虽然不是构造方法,但是子类实现onCrreate方法的时候的super.onCreate(savedInstanceState);就会指向这里的代码
Log.i("ResquestWithTakePotoActivity", "拍照时进入到ResquestWithTakePotoActivity");
}
protected void enableCrop(boolean flag) {//子类用于设置能否剪切的标志
enableCrop = flag;
}
public void imageChooseItem(boolean isShowSelectPhoto) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.take_photo_select, null);
view.findViewById(R.id.photo_delete).setVisibility(View.GONE);
view.findViewById(R.id.take_photo).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
startActionCamera();//拍照按钮点击后,启动照相机
if (dialog != null) {
dialog.cancel();
}
}
});
if (isShowSelectPhoto) {
view.findViewById(R.id.pic_photo).setVisibility(View.VISIBLE);
view.findViewById(R.id.take_photo_line).setVisibility(View.VISIBLE);
view.findViewById(R.id.pic_photo).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// startImagePick();
SelectPhoto();
if (dialog != null) {
dialog.cancel();
}
}
});
} else {
view.findViewById(R.id.take_photo_line).setVisibility(View.GONE);
view.findViewById(R.id.pic_photo).setVisibility(View.GONE);
}
dialog = DialogUtil.showInfo(this,
getResources().getString(R.string.title_select), view,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}, false, false);
}
public void imageChooseItemWithDelete(int type, final int index,
boolean isSetBitmap) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.take_photo_select, null);
Log.e("hf:issetbitamp", isSetBitmap + "");
if (isSetBitmap) {
view.findViewById(R.id.take_photo_line).setVisibility(View.VISIBLE);
view.findViewById(R.id.photo_delete).setVisibility(View.VISIBLE);
} else {
view.findViewById(R.id.photo_delete).setVisibility(View.GONE);
view.findViewById(R.id.take_photo_line).setVisibility(View.GONE);
}
view.findViewById(R.id.pic_photo).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// startImagePick();
SelectPhoto();
if (dialog != null) {
dialog.cancel();
}
}
});
view.findViewById(R.id.take_photo).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
startActionCamera();
if (dialog != null) {
dialog.cancel();
}
}
});
view.findViewById(R.id.photo_delete).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
deleteImage(index);
if (dialog != null) {
dialog.cancel();
}
}
});
dialog = DialogUtil.showInfo(this,
getResources().getString(R.string.title_select), view,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}, false, false);
}
abstract protected void deleteImage(int index);
private void startActionCamera() {//以startActivityForResult的方式跳转到照相机界面
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// action is
// capture
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//另存图片地址
startActivityForResult(intent, TAKE_PHOTO);
}
private void startImagePick(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", getCropX());
intent.putExtra("aspectY", getCropY());
intent.putExtra("outputX", getCropWidth());
intent.putExtra("outputY", getCropHeight());
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra("scaleUpIfNeeded", true);// 解决当图片大小小于输出大小时,部分机器截取图片黑边
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", false); // no face detection
startActivityForResult(intent, PIC_PHOTO);
}
private void SelectPhoto() {
Intent intent = new Intent();
intent.setClass(this, SelectFromPhotoAlbumActivity.class);
intent.putExtra("TYPE", "single_pic");
intent.putExtra("SELECTED_IMAGES", save_images_arr_paths);
intent.putExtra("SELECTED_IMAGES_ID", save_images_arr_id);
startActivityForResult(intent, GET_PHOTO_RESULT_ID);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
Log.e("hfdd:", imageUri + "");
if (resultCode != Activity.RESULT_CANCELED) {
cropImageUri(imageUri, CROP_BIG_PICTURE);
}
break;
case CROP_BIG_PICTURE:// from crop_big_picture
Log.e("hf:", imageUri + "");
if (imageUri != null) {
Bitmap bitmap = decodeUriAsBitmap(imageUri);
onImageCroped(bitmap);
} else {
return;
}
break;
case PIC_PHOTO:
if (imageUri != null) {
Bitmap bitmap = decodeUriAsBitmap(imageUri);
onImageCroped(bitmap);
} else {
return;
}
break;
case GET_PHOTO_RESULT_ID:
if (data != null) {
ArrayList<Integer> images_arr_id = data
.getIntegerArrayListExtra("select_photos");
ArrayList<String> images_arr_paths = data
.getStringArrayListExtra("select_photos_paths");
Uri uri = Uri.parse("file://" + images_arr_paths.get(0));
startImagePick(uri);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private Bitmap decodeUriAsBitmap(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
}
private void cropImageUri(Uri uri, int requestCode) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", getCropX());
intent.putExtra("aspectY", getCropY());
intent.putExtra("outputX", getCropWidth());
intent.putExtra("outputY", getCropHeight());
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);// 解决当图片大小小于输出大小时,部分机器截取图片黑边
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra("return-data", false);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, requestCode);
}
protected boolean savePicture(Bitmap image, File picture) {
if (!picture.exists()) {
try {
File path = new File(picture.getParent());
if (!path.exists()) {
if (!path.mkdirs()) {
return false;
}
}
if (!picture.createNewFile()) {
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
BufferedOutputStream stream;
try {
stream = new BufferedOutputStream(new FileOutputStream(picture));
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
stream.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
abstract protected int getCropWidth();
abstract protected int getCropHeight();
abstract protected int getCropX();
abstract protected int getCropY();
abstract protected void onImageCroped(Bitmap image);
abstract protected void onImageSelected(Uri uri);
}
public abstract class RequestWithTakePhotoActivity extends BaseActivity {
private static final String IMAGE_UNSPECIFIED = "image/*";
private static final String IMAGE_TMP = "photo_tmp.png";
private static final String IMAGE_TMP_2 = "photo_tmp2.png";
private static final int TAKE_PHOTO = 0;
private static final int PIC_PHOTO = 1;
// private static final int CROP_PHOTO = 2;
private static final int CROP_BIG_PICTURE = 3;
private boolean enableCrop = true;
private static final int MAX_LINE = 480;
private Uri imageUri;
protected PopDialog dialog = null;
private ArrayList<Integer> save_images_arr_id = new ArrayList<Integer>();
private ArrayList<String> save_images_arr_paths = new ArrayList<String>();
public final static int GET_PHOTO_RESULT_ID = 6;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File outFile = new File(getExternalCacheDir() + "/" + IMAGE_TMP);
imageUri = Uri.fromFile(outFile);
//测试后会发现并没有进入到onCreate方法//不对,虽然不是构造方法,但是子类实现onCrreate方法的时候的super.onCreate(savedInstanceState);就会指向这里的代码
Log.i("ResquestWithTakePotoActivity", "拍照时进入到ResquestWithTakePotoActivity");
}
protected void enableCrop(boolean flag) {//子类用于设置能否剪切的标志
enableCrop = flag;
}
public void imageChooseItem(boolean isShowSelectPhoto) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.take_photo_select, null);
view.findViewById(R.id.photo_delete).setVisibility(View.GONE);
view.findViewById(R.id.take_photo).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
startActionCamera();//拍照按钮点击后,启动照相机
if (dialog != null) {
dialog.cancel();
}
}
});
if (isShowSelectPhoto) {
view.findViewById(R.id.pic_photo).setVisibility(View.VISIBLE);
view.findViewById(R.id.take_photo_line).setVisibility(View.VISIBLE);
view.findViewById(R.id.pic_photo).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// startImagePick();
SelectPhoto();
if (dialog != null) {
dialog.cancel();
}
}
});
} else {
view.findViewById(R.id.take_photo_line).setVisibility(View.GONE);
view.findViewById(R.id.pic_photo).setVisibility(View.GONE);
}
dialog = DialogUtil.showInfo(this,
getResources().getString(R.string.title_select), view,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}, false, false);
}
public void imageChooseItemWithDelete(int type, final int index,
boolean isSetBitmap) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.take_photo_select, null);
Log.e("hf:issetbitamp", isSetBitmap + "");
if (isSetBitmap) {
view.findViewById(R.id.take_photo_line).setVisibility(View.VISIBLE);
view.findViewById(R.id.photo_delete).setVisibility(View.VISIBLE);
} else {
view.findViewById(R.id.photo_delete).setVisibility(View.GONE);
view.findViewById(R.id.take_photo_line).setVisibility(View.GONE);
}
view.findViewById(R.id.pic_photo).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// startImagePick();
SelectPhoto();
if (dialog != null) {
dialog.cancel();
}
}
});
view.findViewById(R.id.take_photo).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
startActionCamera();
if (dialog != null) {
dialog.cancel();
}
}
});
view.findViewById(R.id.photo_delete).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
deleteImage(index);
if (dialog != null) {
dialog.cancel();
}
}
});
dialog = DialogUtil.showInfo(this,
getResources().getString(R.string.title_select), view,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}, false, false);
}
abstract protected void deleteImage(int index);
private void startActionCamera() {//以startActivityForResult的方式跳转到照相机界面
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// action is
// capture
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//另存图片地址
startActivityForResult(intent, TAKE_PHOTO);
}
private void startImagePick(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", getCropX());
intent.putExtra("aspectY", getCropY());
intent.putExtra("outputX", getCropWidth());
intent.putExtra("outputY", getCropHeight());
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra("scaleUpIfNeeded", true);// 解决当图片大小小于输出大小时,部分机器截取图片黑边
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", false); // no face detection
startActivityForResult(intent, PIC_PHOTO);
}
private void SelectPhoto() {
Intent intent = new Intent();
intent.setClass(this, SelectFromPhotoAlbumActivity.class);
intent.putExtra("TYPE", "single_pic");
intent.putExtra("SELECTED_IMAGES", save_images_arr_paths);
intent.putExtra("SELECTED_IMAGES_ID", save_images_arr_id);
startActivityForResult(intent, GET_PHOTO_RESULT_ID);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
Log.e("hfdd:", imageUri + "");
if (resultCode != Activity.RESULT_CANCELED) {
cropImageUri(imageUri, CROP_BIG_PICTURE);
}
break;
case CROP_BIG_PICTURE:// from crop_big_picture
Log.e("hf:", imageUri + "");
if (imageUri != null) {
Bitmap bitmap = decodeUriAsBitmap(imageUri);
onImageCroped(bitmap);
} else {
return;
}
break;
case PIC_PHOTO:
if (imageUri != null) {
Bitmap bitmap = decodeUriAsBitmap(imageUri);
onImageCroped(bitmap);
} else {
return;
}
break;
case GET_PHOTO_RESULT_ID:
if (data != null) {
ArrayList<Integer> images_arr_id = data
.getIntegerArrayListExtra("select_photos");
ArrayList<String> images_arr_paths = data
.getStringArrayListExtra("select_photos_paths");
Uri uri = Uri.parse("file://" + images_arr_paths.get(0));
startImagePick(uri);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private Bitmap decodeUriAsBitmap(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
}
private void cropImageUri(Uri uri, int requestCode) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", getCropX());
intent.putExtra("aspectY", getCropY());
intent.putExtra("outputX", getCropWidth());
intent.putExtra("outputY", getCropHeight());
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);// 解决当图片大小小于输出大小时,部分机器截取图片黑边
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra("return-data", false);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, requestCode);
}
protected boolean savePicture(Bitmap image, File picture) {
if (!picture.exists()) {
try {
File path = new File(picture.getParent());
if (!path.exists()) {
if (!path.mkdirs()) {
return false;
}
}
if (!picture.createNewFile()) {
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
BufferedOutputStream stream;
try {
stream = new BufferedOutputStream(new FileOutputStream(picture));
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
stream.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
abstract protected int getCropWidth();
abstract protected int getCropHeight();
abstract protected int getCropX();
abstract protected int getCropY();
abstract protected void onImageCroped(Bitmap image);
abstract protected void onImageSelected(Uri uri);
}
相关文章推荐
- Android异步通信机制简单地理解,Handler是当前线程的消息队列中的一个子队列,而Runable是可以被安排到Handler去运行的接口。 首先在Activity中创建一个继承自Han
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- Android中Activity启动模式详解,可以控制程序按home键后进来还会调用一个自己不喜欢的界面
- Navigation Drawer (ads里面有个可以直接继承此Activity)
- 快到七夕了,分享一个C写的exe,可以直接点击运行的,效果看下图
- Mouse without Borders是微软的一个软件实验项目,这款软件可让鼠标自由在局域网中的多台电脑间移动,键盘输入实现无缝切换,还可以直接用鼠标相互拖拽文件。 如果经常需要同时使用两台以
- 写了一个PHP版本的MONGODB语法解析器,可以通过类似SQL的语法来进行MONGODB的查询,不知道有人需要不,分享一下吧
- App调用系统Camera拍照或者从Gallery选取图片,返回时先显示手机Launcher或者前一个界面,再跳转到当前activity
- 现在很多的手机应用,都可能会直接嵌入一个web页面。这样做的好处:一个是功能更新方便,维护起来容易,只需要维护服务器的页面即可,不需要更新客户端;另一个是功能通用,不仅Android可以用,iOS也可
- 使用 satis 搭建一个私有的 Composer 包仓库 在我们的日常php开发中可能需要使用大量的composer包,大部份都可以直接使用,但在公司内部总有一小部份包是不能公开的,这时候我们就需
- unity 给大家分享一个可以直接将unity中的物体导出成fbx存在项目中的方法(实测pc可用,移动端不可以~~~其他待测~~)
- (原创)多系统间需要对接,我写了一个接口框架。实用性非常强,写出来大家交流。需要的可以直接搬过去用。(第1篇)
- 友盟分享和第三方登录的一个工具类,具体的使用方法直接去友盟官网看,写的非常详细
- 分享一个格式化json的工具类,在日志输入里直接可以很方便的看log了
- c++ 设计一个不能被继承的类,但可以实例化
- 【解答】一个电动模型,每一组电池能让其行驶8分钟,一个充电器能同时给两组电池充电,一组充满需要15分钟,至少准备(?)组电池,(?)个充电器,可以让模型每次行驶完可以立即换电池行驶不用等待。
- 创建存储过程,以部门编号为参数返回该部门的人数及平均工资(返回一个值可以通过函数来实现,但如果要返回多个值,需要使用out或in out模式参数实现)
- 当我把一个"结构体"在类中当做属性后, 在实用中可以直接读取结构体成员, 但不能直接写入.
- 就是在设计界面可以看到的最下面的那个说明 因为我需要在程序中告诉用户这个字段是干什么的
- Android应用是否可以只有一个Service或Broadcast Reciver,而没有Activity?