这次我主要实现调用照相机并且能够在自己设置Activity 上显示此图片,比且当我们照完图片的时候自己可以对照片进行裁剪。
配置文件没有做出改动关键是我们代码的实现,调用手机自带的照相系统,这个最重要的一点就是Intent 的设置就是下面俩句话:
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
xml文件内布局的设置:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试"
android:id="@+id/test"
/>
<ImageView android:id="@+id/iv01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ImageView>
</LinearLayout>
点击测试之后我们进入系统自带的照相机的那个界面,然后根据情况可以裁减界面,裁剪这块是参考别的书籍上所写的:
下面是主代码:
public class MainActivity extends Activity {
private Button button;
private ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.button=(Button)findViewById(R.id.test);
this.imageView=(ImageView)findViewById(R.id.iv01);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub\
//设置跳动页面 调用系统自带的照相机
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
}
});
}
//开始处理相片 前段显示图片在onActivityResult中处理返回的data
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
if(resultCode!=RESULT_OK)
return;
switch (requestCode)
{
//开始对我们完成的Intent乞求开始做出判断
case 1:
Bitmap bitmap = data.getParcelableExtra("data");
if (bitmap!=null)
{
dealBitmap(bitmap);
}
case 0:
Bitmap bitmap2 = data.getParcelableExtra("data");
if(bitmap2!=null){
//显示在我们的Activity上
imageView.setImageBitmap(bitmap2);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
//开始对我们照完的照片进行处理
protected void dealBitmap(Bitmap bitmap){
//调用下面的函数开始对图片进行修改
Intent intent = getDealImageIntent(bitmap);
startActivityForResult(intent, 0);
}
public static Intent getDealImageIntent(Bitmap bitmap)
{
Intent intent = new Intent("com.android.camera.action.CROP");
//设置裁时的一些参数
intent.setType("image/*");
intent.putExtra("data", bitmap);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 128);
intent.putExtra("outputY", 128);
intent.putExtra("return-data", true);
return intent;
}
}
总体来说难度并不是很大但关键是我们调用照相机之后的处理,以及我们对
Intent使用一些技巧!
本文介绍如何在Android应用中调用系统摄像头并拍摄照片,同时提供了对拍摄后的图片进行裁剪的方法。
7035

被折叠的 条评论
为什么被折叠?



