i'm building an application in which i need to take a photo from a custom camera and the view is inverted or rotated (when i move it up and down the picture moves sideways instead).
any idea why that might be and how to resolve this?
this is the code for the camera:
class Preview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
Preview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback开发者_Go百科() {
// Called for each frame previewed
public void onPreviewFrame(byte[] data, Camera camera) {
Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
Preview.this.invalidate();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
camera.startPreview();
the code that calls the preview:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.solve_capture);
preview = new Preview(this); // <3>
((FrameLayout) findViewById(R.id.preview)).addView(preview); // <4>
buttonClick = (Button) findViewById(R.id.buttonCapture);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) { // <5>
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
buttonFocus = (Button) findViewById(R.id.buttonFocus);
buttonFocus.setOnClickListener(new OnClickListener() {
public void onClick(View v) { // <5>
preview.camera.autoFocus(new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
Camera.Parameters camParam = camera.getParameters();
camParam.setFocusMode(Parameters.FOCUS_MODE_AUTO);
camera.setParameters(camParam);
}
});
}
});
Log.d(TAG, "onCreate'd");
}
Ok, i found a way that works for me: i've set (in the manifest) the Activity that uses Preview (meaning, the second part of code i provided) orientation to "landscape" and changed the layout to fit the landscape mode. now the camera looks fine. but this means that the camera Activity will always be in landscape (which is good enough for me).
精彩评论