Reputation: 273
I've set up a simple camera app using code I found online. I have modified it a lot until it works as I like. However, the image it takes ends up being stretched vertically for some reason even though the preview looks fine. I am running this app on a galaxy nexus
Here is what it looks like when taken with the built in camera app:
And here is what my app's image looks like:
Any idea why this is? Both images are the same dimension
Thanks in advance. My code is below
public class CameraActivity extends Activity implements SurfaceHolder.Callback,
OnClickListener {
private static final String TAG = "CameraTest";
Camera mCamera;
boolean mPreviewRunning = false;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.i(TAG, "onCreate");
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.surface_camera);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
mSurfaceView.setOnClickListener(this);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
Camera.PictureCallback mJpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
if (imageData != null) {
handleJPEGData(imageData);
mCamera.startPreview();
}
}
};
Camera.PictureCallback mRawCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferQualityOverSpeed = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
Log.i(TAG, "Saving picture");
MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "1234", "124");
Log.i(TAG, "Picture Saved");
}
};
private void handleJPEGData(byte[] data) {
Log.i(TAG, "Saving picture");
ContentValues vals = new ContentValues();
vals.put(Images.Media.DATE_TAKEN, new Date().getTime());
vals.put(Images.Media.DATE_ADDED, new Date().getTime());
vals.put(Images.Media.MIME_TYPE, "images/jpeg");
// vals.put(Images.Media., value)
Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.i(TAG, "Picture Saved");
mRawCallback.onPictureTaken(data, mCamera);
}
@Override
protected void onResume() {
Log.i(TAG, "onResume");
super.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
protected void onStop() {
Log.i(TAG, "onStop");
super.onStop();
}
public void surfaceCreated(SurfaceHolder holder) {
Log.i(TAG, "surfaceCreated");
mCamera = Camera.open();
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.i(TAG, "surfaceChanged");
// XXX stopPreview() will crash if preview is not running
if (mPreviewRunning) {
mCamera.stopPreview();
}
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(1920, 1080);
p.setAntibanding(Camera.Parameters.ANTIBANDING_AUTO);
p.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
p.setJpegQuality(100);
p.setPictureFormat(ImageFormat.JPEG);
p.setPictureSize(2592,1944);
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i(TAG, "surfaceDestroyed");
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
}
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
public void onClick(View arg0) {
mCamera.takePicture(null, null, mJpegCallback);
}
}
Upvotes: 3
Views: 1018
Reputation: 273
Figured it out. Its because the preview dimensions are different from the picture dimensions. Its kind of stupid that it is this way, but it is. So the solution is to make them the same aspect ratio. In other news however, it appears that this is fixed in Jelly Bean
Upvotes: 2