全部博文(155)
分类: 嵌入式
2010-10-19 16:24:15
使用android拍照,实现起来比较简单,直接调用手机内置的摄像头。调用代码android的demo程序中已经给出。我在上面稍微做了些修改。下面我们看下具体代码。
源代码见:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;public class PhotographActivity extends Activity {
/** Called when the activity is first created. */
private String logTag = "Exception";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
} catch (Exception e) {
Log.v(logTag, e.getMessage());
}
}protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
try {
if (requestCode != 0) {
return;
}
super.onActivityResult(requestCode, resultCode, data);Bundle extras = data.getExtras();
Bitmap b = (Bitmap) extras.get("data");
Intent intent = new Intent();//得到图片后,我们在另一个activity中显示
intent.setClass(this, ShowImageActivity.class);
intent.putExtra("image",b);
this.startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
Log.v(logTag, e.getMessage());
}
}}
新建一个显示图片的activity。这样,在这个activity中就可以对图片做其他更多的操作。
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;public class ShowImageActivity extends Activity {
private String logTag = "exception";
private ImageView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show);try {
view = (ImageView) findViewById(R.id.view);
Bundle bundle = this.getIntent().getExtras();
Bitmap b = bundle.getParcelable("image");
view.setImageBitmap(b);
//setContentView(view);
} catch (Exception e) {
Log.v(logTag, e.getMessage());
throw new RuntimeException(e);
}
}
}
效果如下图:
拍照
显示拍照后的图片