我尝试了几件事来试图让相机预览以肖像形式出现在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开始,这是可用的:
public final void setDisplayOrientation(int degrees)
即清单中的肖像:
public void surfaceCreated(SurfaceHolder holder) { mCamera = Camera.open(); mCamera.setDisplayOrientation(90);
我有一个工作解决方案的纵向模式在2.1(在Desire上测试)工作可能更少.
活动屏幕方向设置为纵向.(机器人:screenOrientation = "纵向")
相机参数:
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必须与手机预览尺寸兼容,通常是屏幕分辨率.
有趣的欲望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 { } } };