[英]Android Camera in Portrait on SurfaceView
我嘗試了幾種方法來嘗試使照相機預覽以縱向顯示在SurfaceView
。 沒事。 我正在測試具有2.0.1的Droid。 我試過了:
1)通過以下方式強制使布局為縱向: this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
2)使用
Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
parameters.setRotation(90);
camera.setParameters(parameters);
還有什么我可以嘗試的嗎? 如果這是Android或手機中的錯誤,我該如何確定情況如此,以便我有證據通知客戶?
謝謝,Prasanna
從API lvl 8開始,此功能可用:
公共最終空白setDisplayOrientation(以度為單位)
即與清單中的肖像:
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
我有一個工作在2.1(在Desire上測試)下的肖像模式的可行解決方案。
活動屏幕方向設置為縱向。 (android:screenOrientation =“ portrait”)
相機參數:
Camera.Parameters p = mCamera.getParameters();
p.set("jpeg-quality", 100);
p.set("orientation", "landscape");
p.set("rotation", 90);
p.setPictureFormat(PixelFormat.JPEG);
p.setPreviewSize(h, w);// here w h are reversed
mCamera.setParameters(p);
並且圖像將是縱向的。
用於相機的SurfaceHolder必須具有與手機預覽尺寸(通常為屏幕分辨率)兼容的尺寸。
有趣的Desire 2.2無法運作...這里是解決方法:
At surfaceCreated(..) or when you have this line
camera = Camera.open();
add
camera.setDisplayOrientation(90);//only 2.2>
Camera.Parameters p = camera.getParameters();
p.set("jpeg-quality", 100);
p.setRotation(90);
p.setPictureFormat(PixelFormat.JPEG);
p.setPreviewSize(h, w);
camera.setParameters(p);
您可以嘗試一下(適用於2.2或更低版本)。 在這里,我先旋轉圖像,然后再將其保存到SD卡中。 但這僅適用於肖像模式。 如果必須同時使用這兩種模式,則應在拍攝圖像之前檢查相機方向並進行一些檢查。
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
imageFilePath = getFilename();
InputStream is = new ByteArrayInputStream(data);
Bitmap bmp = BitmapFactory.decodeStream(is);
// Getting width & height of the given image.
if (bmp != null){
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
outStream = new FileOutputStream
(String.format(imageFilePath,System.currentTimeMillis()));
outStream.write(byteArray);
outStream.close();
} else {
outStream = new FileOutputStream
(String.format(imageFilePath,System.currentTimeMillis()));
outStream.write(data);
outStream.close();
}
preview.camera.startPreview();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
};
在許多當前設備(包括G1和Droid)上都無法做到這一點。 在這里查看相關的錯誤報告:
另請參閱此處的一位Android工程師(Dave)的評論:
Roman給出的問題線程鏈接具有我現在正在使用的可行解決方案。
在此處找到它: http : //code.google.com/p/android/issues/detail?id=1193#c26
除非您需要明確地進行設置,否則無需為定向設置任何參數。 默認情況下,它支持此功能。 就我而言,我有一個“活動”,而在該活動之上,我有一個攝影機視圖,因此我沒有為攝影機屬性設置任何方向,而是為該活動在Manifest文件中將其方向設置為縱向。 現在,該應用程序看起來很好用。 可能會對某些人有所幫助。
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.