本文介绍了安卓:对HTC EVO(的Andr​​oid 2.1或2.2)相机preVIEW方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,它依赖于摄像头API,使用HTC EVO作为我的测试设备。无论我到目前为止已经试过,相机preVIEW期待权的唯一时间是在横向模式下(90度旋转,要具体)。它好像是没有办法在纵向模式时,正确定向preVIEW(0度旋转)。

在设备上的默认摄像头应用程序(的HTC Sense)允许任何类型的旋转没有任何问题,所以我知道,没有硬件限制。我甚至下载了HTC的开发商部分源$ C ​​$ C,但是这一切都用C - 内核的东西,显然

任何人都可以点我解决这个问题的正确方向?有没有办法在Android 2.1系统或2.2正确旋转preVIEW?

感谢您。

P.S。这里是code我使用,在情况下,它可以帮助...

 包spikes.cameraSpike03;进口的java.util.List;进口android.app.Activity;
进口android.graphics.PixelFormat;
进口android.hardware.Camera;
进口android.hardware.Camera.Size;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.Surface;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
进口android.view.Window;
进口android.view.WindowManager;公共类MainActivity扩展活动实现SurfaceHolder.Callback {
 私有静态最后弦乐LOG_TAG =spikes.cameraSpike03 - MainActivity 私人相机_camera;
 私人布尔_ previewIsRunning = FALSE; 私人SurfaceView _svCameraView;
 私人SurfaceHolder _surfaceHolder;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        。getWindow()和setFormat(PixelFormat.TRANSLUCENT);
        getWindow()setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN)。        的setContentView(R.layout.main);        _svCameraView =(SurfaceView)findViewById(R.id.svCameraView);        _surfaceHolder = _svCameraView.getHolder();
        _surfaceHolder.addCallback(本);
        _surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    } @覆盖
 公共无效surfaceChanged(SurfaceHolder架,INT格式,诠释的宽度,高度INT){
  如果(_ previewIsRunning){
   _camera.stop preVIEW();
  }  尝试{
   Camera.Parameters参数= _camera.getParameters();   //获取最佳preVIEW大小,所以设置参数时,我们没有得到一个异常
   清单<尺寸和GT;支持previewSizes = parameters.getSupported previewSizes();
   尺寸优化previewSize = getOptimal previewSize(支持previewSizes,宽,高);
   parameters.set previewSize(最佳previewSize.width,最佳previewSize.height);   _camera.setParameters(参数);   _camera.set previewDisplay(支架);
  }
  赶上(例外前){
   ex.printStackTrace();
   Log.e(LOG_TAG,ex.toString());
  }  _camera.start preVIEW();
  _ previewIsRunning = TRUE;
 } @覆盖
 公共无效surfaceCreated(SurfaceHolder持有人){
  _camera = Camera.open();
 } @覆盖
 公共无效surfaceDestroyed(SurfaceHolder持有人){
  _camera.stop preVIEW();
  _ previewIsRunning = FALSE;
  _camera.release();
 } 私人尺寸getOptimal previewSize(列表<尺寸和GT;的大小,INT W,INT高){
        最终双ASPECT_TOLERANCE = 0.05;
        双targetRatio =(双)W / H;
        如果(大小== NULL)返回NULL;        大小optimalSize = NULL;
        双minDiff = Double.MAX_VALUE;        INT targetHeight = H;        //尝试找到一个大小匹配纵横比和尺寸
        对于(尺寸大小:大小){
            双率=(双)size.width / size.height;
            如果(Math.abs(比 - targetRatio)GT; ASPECT_TOLERANCE)继续;
            如果(Math.abs(size.height - targetHeight)LT; minDiff){
                optimalSize =大小;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }        //找不到一个长宽比匹配,忽略此要求
        如果(optimalSize == NULL){
            minDiff = Double.MAX_VALUE;
            对于(尺寸大小:大小){
                如果(Math.abs(size.height - targetHeight)LT; minDiff){
                    optimalSize =大小;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        返回optimalSize;
    }}


解决方案

尝试锁定活动的方向水平是否与您的设计适合,或与这些戏:

 私人android.hardware.Camera mCamera;

...

  mCamera.setDisplayOrientation(90);
params.setRotation(90);
mCamera.setParameters(PARAMS);

但我认为setDisplayOrientation可能是2.2只...

I am developing an Android application that relies on the camera API, using an HTC EVO as my testing device. No matter what I've tried so far, the only time the camera preview looks right is in landscape mode (90 degrees rotation, to be specific). It seems as though there is no way to orient the preview correctly when in portrait mode (0 degrees rotation).

The default camera application on the device (for HTC Sense) allows for any kind of rotation without any problem, so I know that there is no hardware limitation. I even downloaded some source code from HTC's developer site, but it's all in C - kernel stuff, apparently.

Can anyone point me in the right direction for solving this problem? Is there a way to rotate the preview correctly in Android 2.1 or 2.2?

Thank you.

P.S. Here is the code I'm using, in case it helps...

package spikes.cameraSpike03;

import java.util.List;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity implements SurfaceHolder.Callback {
 private static final String LOG_TAG = "spikes.cameraSpike03 - MainActivity";

 private Camera _camera;
 private boolean _previewIsRunning = false;

 private SurfaceView _svCameraView;
 private SurfaceHolder _surfaceHolder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        _svCameraView = (SurfaceView)findViewById(R.id.svCameraView);

        _surfaceHolder = _svCameraView.getHolder();
        _surfaceHolder.addCallback(this);
        _surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  if(_previewIsRunning){
   _camera.stopPreview();
  }

  try{
   Camera.Parameters parameters = _camera.getParameters();

   //Get the optimal preview size so we don't get an exception when setting the parameters
   List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
   Size optimalPreviewSize = getOptimalPreviewSize(supportedPreviewSizes, width, height);
   parameters.setPreviewSize(optimalPreviewSize.width, optimalPreviewSize.height);

   _camera.setParameters(parameters);

   _camera.setPreviewDisplay(holder);
  }
  catch(Exception ex){
   ex.printStackTrace();
   Log.e(LOG_TAG, ex.toString());
  }

  _camera.startPreview();
  _previewIsRunning = true;
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  _camera = Camera.open();
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  _camera.stopPreview();
  _previewIsRunning = false;
  _camera.release();
 }

 private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.05;
        double targetRatio = (double) w / h;
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

}
解决方案

Try locking the orientation of the Activity in horizontal if that fits with your design, or have a play with these:

private android.hardware.Camera mCamera;

...

mCamera.setDisplayOrientation(90);
params.setRotation(90);
mCamera.setParameters(params);

But I think setDisplayOrientation might be 2.2 only...

这篇关于安卓:对HTC EVO(的Andr​​oid 2.1或2.2)相机preVIEW方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 00:32