Skip to content

Instantly share code, notes, and snippets.

@msdx
Last active January 8, 2016 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msdx/0d62bc168945a4af2d67 to your computer and use it in GitHub Desktop.
Save msdx/0d62bc168945a4af2d67 to your computer and use it in GitHub Desktop.
Open camera.
private void openCamera() {
if (mCamera == null) {
try {
mCamera = Camera.open();
} catch (RuntimeException e) {
mCamera = null;
}
}
if (mCamera == null) {
MessageProxy.showError(this, R.string.msg_camera_invalid);
finish();
return;
}
final Camera.Parameters cameraParams = mCamera.getParameters();
cameraParams.setPictureFormat(ImageFormat.JPEG);
cameraParams.setRotation(90);
cameraParams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.setParameters(cameraParams);
Camera.Size size = cameraParams.getPreviewSize();
ViewGroup.LayoutParams params = mSurfaceView.getLayoutParams();
params.height = mSurfaceView.getWidth() * size.width / size.height;
mSurfaceView.setLayoutParams(params);
if (mIsSurfaceReady) {
startPreview();
}
}
private void startPreview() {
if (mCamera == null) {
return;
}
try {
mCamera.setPreviewDisplay(mSurfaceView.getHolder());
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
BugReport.report(e);
}
}
/**
* 拍照
*/
private void takePhoto() {
if (mCamera == null || mWaitForTakePhoto) {
return;
}
mWaitForTakePhoto = true;
mCamera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
onTakePhoto(data);
mWaitForTakePhoto = false;
}
});
}
private void onTakePhoto(byte[] data) {
FileOutputStream fos = null;
try {
String tempPath = mOutput + "_";
fos = new FileOutputStream(tempPath);
fos.write(data);
fos.flush();
PhotoActionHelper.clipImage(this)
.extra(getIntent())
.output(mOutput).input(tempPath)
.requestCode(Const.Request.CLIP_IMAGE).start();
} catch (Exception e) {
BugReport.report(e);
} finally {
IOUtils.close(fos);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment