public class CameraActivity extends Activity {
CameraView cameraView;
Button btnOpenCamera, btnShowPhoto, btnHelp;
int markView = 0;
boolean isMarkImageShow = false;
/*用来标识请求照相功能的activity*/
private static final int CAMERA_WITH_DATA = 3023;
/*用来标识请求gallery的activity*/
private static final int PHOTO_PICKED_WITH_DATA = 3021;
private File mCurrentPhotoFile;//照相机拍照得到的图片
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.camera);
cameraView = new CameraView(this);
initButtons();
}
public void initButtons(){
btnOpenCamera = (Button)findViewById(R.id.camera_open);
btnShowPhoto = (Button)findViewById(R.id.camera_show_photo);
btnHelp = (Button)findViewById(R.id.camera_help);
btnOpenCamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// setContentView(cameraView);
// cameraView.requestFocus();
// cameraView.setFocusable(true);
// Intent intent = new Intent();
// intent.setClass(CameraActivity.this, CameraOpenActivity.class);
// intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
// startActivity(intent);
doTakePhoto();
}
});
btnShowPhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Intent intent = new Intent();
// intent.setClass(CameraActivity.this, ShowPictureActivity.class);
// startActivity(intent);
// finish();
doPickPhotoFromGallery();
}
});
btnHelp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
/**
* 拍照获取图片
*
*/
protected void doTakePhoto() {
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + "ToolBoxfiles";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
if (dir.exists()) {
try {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyy-MM-dd_HH-mm-ss");
String fileName = dateFormat.format(date) + ".jpg";
mCurrentPhotoFile = new File(path, fileName);
Uri u = Uri.fromFile(mCurrentPhotoFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE,
null);
intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
startActivityForResult(intent, CAMERA_WITH_DATA);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, "--doTakePhoto--", Toast.LENGTH_LONG).show();
}
}
}
protected void doCropPhoto(File f) {
try {
// 启动gallery去剪辑这个照片
final Intent intent = getCropImageIntent(Uri.fromFile(f));
startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
} catch (Exception e) {
Toast.makeText(this, "--doCropPhoto--", Toast.LENGTH_LONG).show();
}
}
/**
* Constructs an intent for image cropping. 调用图片剪辑程序
*/
public static Intent getCropImageIntent(Uri photoUri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(photoUri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 80);
intent.putExtra("outputY", 80);
intent.putExtra("return-data", true);
return intent;
}
// 请求Gallery程序
protected void doPickPhotoFromGallery() {
try {
// Launch picker to choose photo for selected contact
final Intent intent = getPhotoPickIntent();
startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "doPickPhotoFromGallery",
Toast.LENGTH_LONG).show();
}
}
// 封装请求Gallery的intent
public static Intent getPhotoPickIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 80);
intent.putExtra("outputY", 80);
intent.putExtra("return-data", true);
return intent;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case PHOTO_PICKED_WITH_DATA: {// 调用Gallery返回的
final Bitmap photo = data.getParcelableExtra("data");
// ImageView imageView = new ImageView(this);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(photo);
// this.setContentView(imageView);
// isMarkImageShow = true;
// 下面就是显示照片了
// System.out.println(photo);
//缓存用户选择的图片
// img = getBitmapByte(photo);
// mEditor.setPhotoBitmap(photo);
// System.out.println("set new photo");
break;
}
case CAMERA_WITH_DATA: {// 照相机程序返回的,再次调用图片剪辑程序去修剪图片
doCropPhoto(mCurrentPhotoFile);
break;
}
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
if(isMarkImageShow){
// isMarkImageShow = false;
// this.setContentView(R.layout.camera);
}else{
Intent intent = new Intent();
intent.setClass(CameraActivity.this, ToolBoxActivity.class);
startActivity(intent);
finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
自己写的照相机,还有待改进
public class CameraOpenActivity extends Activity {
/** Called when the activity is first created. */
private SurfaceView surfaceView;
private Button buttonTakePhoto, buttonGoBack, buttonShowPicture;
private Camera camera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 设置横屏
// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_BEHIND);// 设置横屏
// requestWindowFeature(Window.FEATURE_NO_TITLE);// 没有标题
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
// WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);// 屏幕亮度的持续
setContentView(R.layout.camera_open);
surfaceView = (SurfaceView) this.findViewById(R.id.surface_view);
// surfaceView.set
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// surface.getHolder().setFixedSize(176, 144);
surfaceView.getHolder().addCallback(new SurfaceCallback());
initButtons();
}
public void initButtons() {
buttonTakePhoto = (Button) findViewById(R.id.button_take_photo);
buttonTakePhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 停止预览
takePic();
}
});
buttonShowPicture = (Button) findViewById(R.id.button_show_picture);
buttonShowPicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
buttonGoBack = (Button) findViewById(R.id.button_go_back);
buttonGoBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(CameraOpenActivity.this, CameraActivity.class);
startActivity(intent);
finish();
}
});
}
private class SurfaceCallback implements Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
// 获取相机参数
Camera.Parameters parameters = camera.getParameters();
// 设置照片大小
parameters.setPreviewSize(width, height);
// 设置照片格式
parameters.setPictureFormat(PixelFormat.JPEG);
// 设置相机参数
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
// 停止预览
camera.stopPreview();
// 释放相机资源
if (camera != null)
camera.release();
camera = null;
}
}
// 执行保存照片任务
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
new SavePictureTask().execute(data);
camera.startPreview();
}
};
class SavePictureTask extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... params) {
// TODO Auto-generated method stub
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + "ToolBoxfiles";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
if (dir.exists()) {
try {
File file = new File(path, System.currentTimeMillis()
+ ".jpg");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(params[0]);
outputStream.close();
camera.startPreview();
} catch (Exception e) {
// TODO: handle exception
}
}
return null;
}
}
// 拍照方法
public void takePic() {
// 停止预览
camera.stopPreview();
// 拍照
camera.takePicture(null, null, pictureCallback);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent intent = new Intent();
intent.setClass(CameraOpenActivity.this, CameraActivity.class);
startActivity(intent);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}