我正在編寫一個Android應用程序,當用戶單擊按鈕時,可以從相機保存JPEG快照。不幸的是,當我看着JPEG文件時,我的代碼看起來已經損壞。它似乎是由我調用parameters.setPreviewSize(請參閱下面的代碼片段)引起的 - 如果我刪除它,則圖像可以很好地保存;但是沒有它,我無法設置預覽大小,並且setDisplayOrientation在沒有它的情況下似乎也沒有效果。Android:從相機保存的Jpeg看起來損壞
我的應用程序針對的API級別8(Android 2.2),我正在調試HTC Desire HD。不太確定我在這裏做錯了什麼......任何幫助將非常感激!
乾杯, 斯科蒂
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.
Camera.Parameters parameters = mCamera.getParameters();
Camera.Size size = getBestPreviewSize(w,h);
// This next call is required in order for preview size to be set and
// setDisplayOrientation to take effect...
// Unfortunately it's also causing JPEG to be created wrong
parameters.setPreviewSize(size.width, size.height);
parameters.setPictureFormat(ImageFormat.JPEG);
mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
}
// This is the snapshot button event handler
public void onSnapshotButtonClick(View target) {
//void android.hardware.Camera.takePicture(ShutterCallback shutter,
// PictureCallback raw, PictureCallback jpeg)
mPreview.mCamera.takePicture(null, null, mPictureCallback);
}
// This saves the camera snapshot as a JPEG file on the SD card
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
if (imageData != null) {
FileOutputStream outStream = null;
try {
String myJpgPath = String.format(
"/sdcard/%d.jpg", System.currentTimeMillis());
outStream = new FileOutputStream(myJpgPath);
outStream.write(imageData);
outStream.close();
Log.d("TestApp", "onPictureTaken - wrote bytes: "
+ imageData.length);
c.startPreview();
Toast.makeText(getApplicationContext(), String.format("%s written", myJpgPath), Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
};
也適用於我,但只適用於HTC Desire HD。 jpeg在Samsung Galaxy S和模擬器上可以很好地保存。 – 2011-05-14 22:11:42
@jA_cOp - 感謝你,至少我知道這不是我錯了! Android源代碼中的相機應用程序工作正常,因此我將對此進行研究並找出差異。 – Scottie 2011-05-16 10:14:06
你解決了這個問題嗎?我碰到同樣的問題,只有HTC Desire HD – Ikky 2011-06-07 13:15:28