вот мой код для класса обратного вызова поверхностного держателя
class Preview implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Preview(Context context) {
// InstaImageButtonll a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
try {
mHolder = sview.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Preview "+e.toString()+"\n"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
try {
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(holder);
Camera.Parameters params= mCamera.getParameters();
params.set("jpeg-quality", 72);
params.setPictureFormat(PixelFormat.JPEG);
params.setPictureSize(480, 640);
mCamera.setParameters(params);
} catch (Exception e) {
Toast.makeText(TestCameraOverlay.this,
" surfaceCreated" + e.getMessage(), Toast.LENGTH_LONG)
.show();
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
try {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"surfaceDestroyed "+ e.toString()+"\n"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
try {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(w, h);
mCamera.setParameters(parameters);
mCamera.startPreview();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"surfaceChanged "+ e.toString()+"\n"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
и вот мой код для сохранения снятого изображения на SDCard.
Camera.PictureCallback mPictureCallback = новая камера.PictureCallback () {
public void onPictureTaken(byte[] imageData, Camera c) {
try {
File fileOnSD=Environment.getExternalStorageDirectory();
File file=new File(fileOnSD.getAbsolutePath(), "testImage.jpeg");
Toast.makeText(getApplicationContext(),file.getAbsolutePath()+"\n"+imageData, Toast.LENGTH_LONG).show();
FileOutputStream fout=new FileOutputStream(file, false);
fout.write(imageData);
Thread.sleep(2000);
mCamera.startPreview();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"onPictureTaken " +e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
};
Я не понимаю, почему изображение хранится в другой ориентации, чем ориентация снятого изображения.