本文共 4279 字,大约阅读时间需要 14 分钟。
package com.ouling.ex_camera;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class Ex_cameraActivity extends Activity { Button btn_sys, btn_tocamera; ImageView image; String strImgPath = "/sdcard/syscamera.jpg";// 拍照保存文件路径 final int RESULT_CAMERA = 1;// 标识有系统拍照界面返回 final int RESULT_SELF=2;//标识自定义拍照 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); image = (ImageView) findViewById(R.id.img); btn_sys = (Button) findViewById(R.id.sys_camera); btn_tocamera=(Button)findViewById(R.id.self_camera); btn_sys.setOnClickListener(listener); btn_tocamera.setOnClickListener(listener); } OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.sys_camera: // 调用系统拍照功能 cameraMethod(); break; case R.id.self_camera: Intent intent=new Intent(Ex_cameraActivity.this, Self_camera.class);// startActivity(intent); startActivityForResult(intent, RESULT_SELF); Log.i("self_camera","self_camera startActivityForResult"); break; default: break; } } }; // 调用系统拍照 private void cameraMethod() { Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File out = new File(strImgPath); Uri uri = Uri.fromFile(out); imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); //startActivityForResult(imageCaptureIntent, RESULT_CAMERA); //final int TAKE_PICTURE = 1;//为了表示返回方法中辨识你的程序打开的相机 //关键是这里:startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), TAKE_PICTURE); startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), RESULT_CAMERA); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_CAMERA) { if (resultCode == RESULT_OK) { // 拍照图像显示 try { //Bitmap bm = (Bitmap) data.getExtras().get("data"); //Bitmap bm = (Bitmap) data.getExtras().getParcelable("data"); Bundle bundle; if (data==null) { Log.i("data null","data=null"); //return; bundle=this.getIntent().getExtras(); }else { bundle=data.getExtras(); } if (bundle==null) { Log.i("bundle null","bundle=null"); return; } Bitmap bm = (Bitmap)bundle.get("data"); if (bm==null) { Log.i("bmnull","bm=null"); } image.setImageBitmap(bm);// 图像显示在ImageView视图中 File myCaptureFile = new File(strImgPath); try { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(myCaptureFile)); /* 采用压缩转档方法 */ bm.compress(Bitmap.CompressFormat.JPEG, 80, bos); /* 调用flush()方法,更新BufferStream */ bos.flush(); /* 结束OutputStream */ bos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(this, "没有找到照片文件", 1000).show(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(this, e.toString(), 1000).show(); } }catch(Exception e) { Log.i("bmException",e.toString()); } } } else if (requestCode==RESULT_SELF) { if (resultCode==RESULT_OK) { Log.i("自定义拍照","resultCode==RESULT_OK"); try { //显示图片 String pathString=(String) data.getExtras().get("PATH"); Bitmap bitmap=BitmapFactory.decodeFile(pathString); image.setImageBitmap(bitmap); System.out.println("自定义拍照显示"); }catch(Exception e) { Log.i("自定义拍照Exception",e.toString()); } } } }}
转载地址:http://vbkib.baihongyu.com/