我使用在线找到的代码设置了一个简单的相机应用程序。我已经修改了很多,直到它按照我喜欢的方式工作。但是,即使预览看起来很好,它所拍摄的图像也会因某种原因垂直拉伸。我在Galaxy nexus上运行这个应用程序
以下是使用内置相机应用程序时的样子:
以下是我的应用图片的样子:
知道为什么会这样吗?两个图像都是相同的维度
提前致谢。我的代码在
之下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);
}
}
答案 0 :(得分:2)
想出来。这是因为预览尺寸与图片尺寸不同。这是一种愚蠢的方式,但是确实如此。因此解决方案是使它们具有相同的宽高比。然而,在其他新闻中,似乎这是在果冻豆中修复的