我有什么:我有一个表面视图,我在拍摄图像
我要做的是:
public class FrgCamera extends Fragment implements SurfaceHolder.Callback {
private Unbinder unbinder;
private String mFileUriCreated;
@BindView(R.id.surfaceview) SurfaceView surfaceview;
private SurfaceHolder surfaceHolder;
private Camera camera;
private boolean previewing = false;
private Camera.PictureCallback jpegCallback;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return init(inflater, container);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initOnActivityCreated();
}
@Override
public void onDestroyView() {
super.onDestroyView();
stopTheCamera(camera);
unbinder.unbind();
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
initSurfaceView();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
refreshCamera();
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
stopTheCamera(camera);
}
@OnClick(R.id.surfaceview)
public void stopcamerapreview(View view) {
camera.takePicture(null, null, jpegCallback);
}
/**
* INIT OnCreateView
*/
private View init(LayoutInflater inflater, ViewGroup container) {
View rootView = inflater.inflate(R.layout.frg_camera, container, false);
ButterKnife.bind(this, rootView);
unbinder = ButterKnife.bind(this, rootView);
return rootView;
}
/**
* INIT OnActivityCreated
*/
private void initOnActivityCreated() {
getActivity().getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceHolder = surfaceview.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
try {
FileUtil imgDevice = new FileUtil(getActivity());
mFileUriCreated = imgDevice.createFile(data);
} catch (IOException e) {
Timber.d(e, e.getMessage());
}
refreshCamera();
}
};
}
/**
* Refresh Camera
*/
public void refreshCamera() {
if (surfaceHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
camera.stopPreview();
} catch (Exception e) {
Timber.d(e, e.getMessage());
}
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
Timber.d(e, e.getMessage());
}
}
/**
* Initialize Surface view
*/
private void initSurfaceView() {
try {
// open the camera
camera = Camera.open();
camera.setParameters(setCameraParams());
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
Timber.d(e, e.getMessage());
return;
}
}
/**
* SET the camera params for the device
*/
private Camera.Parameters setCameraParams() {
Camera.Parameters param = camera.getParameters();
ImgParmUtil imgParams = new ImgParmUtil(param);
return imgParams.getImageParam();
}
/**
* STOP the camera
*/
private void stopTheCamera(Camera camera) {
if (camera != null && this.previewing) {
camera.stopPreview();
camera.release();
previewing = false;
}
}
}