首页 \ 问答 \ Android:HTC EVO上的相机预览定位(Android 2.1或2.2)(Android: Camera Preview Orientation on HTC EVO (Android 2.1 or 2.2))

Android:HTC EVO上的相机预览定位(Android 2.1或2.2)(Android: Camera Preview Orientation on HTC EVO (Android 2.1 or 2.2))

我正在开发一个依赖于相机API的Android应用程序,使用HTC EVO作为我的测试设备。 无论我到目前为止尝试过什么,相机预览看起来正确的唯一时间是横向模式(旋转90度,具体而言)。 在纵向模式(0度旋转)下,似乎无法正确定位预览。

设备上的默认摄像头应用程序(用于HTC Sense)允许任何类型的旋转而没有任何问题,因此我知道没有硬件限制。 我甚至从HTC的开发者网站上下载了一些源代码,但显然它都是C内核的东西。

任何人都可以指出我正确的方向来解决这个问题吗? 有没有办法在Android 2.1或2.2中正确旋转预览?

谢谢。

PS这是我正在使用的代码,如果它有帮助......

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;
    }

}

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;
    }

}

原文:https://stackoverflow.com/questions/4727957
更新时间:2022-06-27 18:06

最满意答案

假设你有一个表格,例如:

<form name="formname" action="script.php" method="post">

...

您可以将操作更改为:

http://www.site.com/script.php甚至是本地脚本的路径。

这样,即使您在本地运行表单,它仍然会发布到操作,无论它在哪里。

如果您的表单托管在互联网上,并且您尝试将其发布到本地操作,则无法使用。


Say you have a form, for example:

<form name="formname" action="script.php" method="post">

...

You can change the action to:

http://www.site.com/script.php or even the path to the script locally.

That way even if you are running the form locally, it will still post to the action, no matter where it is.

If your form is hosted on the internet, and you try and post it to a local action, it will not work.

相关问答

更多
  • @ Adetona77:如果您的www文件夹包含文件夹名称adetona和文件名test.php,如下所示: www ---test.php ---adetona 使用Laragon,您可以通过以下方式访问test.php : HTTP://localhost/test.php 对于文件夹adetona ,您有2个选项: 通过localhost访问它: http:// localhost / adetona 通过漂亮网址访问: http : //adetona.dev 选项2是Laragon的强大功能 ...
  • 您需要检查文件是否可由Web服务器运行的用户写入,这可能类似于www , apache或类似东西,并且与您的用户不同 - 例如wjones17或其他任何内容。 如果你的权限看起来像 -rw-r--r-- 1 wjones17 group 17 Nov 17 20:02 index.php 那么你的用户有权对该文件进行更改,但是没有其他人做,包括web服务器,以及你的php脚本,它运行为www , apache或其他什么。 就如何解决这个问题而言,您的选择可能在共享环境中受到限制,但是给予所有 ...
  • 您可以使用XAMPP来设置本地Web服务器。 该软件包附带了一个Web服务器(Apache),一个数据库服务器(MySQL)和脚本语言(PHP和Perl)。 它非常受欢迎,特别是对于PHP开发人员IIRC,并且很容易设置和运行。 编辑:Apache允许您从本地文件夹提供网站。 如果要通过Web服务器列出本地文件夹的纯文本内容,只需使用.htaccess文件启用目录列表/索引即可。 You could use XAMPP to setup a local webserver. The package come ...
  • 我认为问题是,如果alert.js脚本正在运行,则无法在上下文中访问您的弹出input元素。 在alert.js的上下文中, document指的是您注入脚本的选项卡的文档。 你可以做这样的事情来使它工作,用这个替换你的popup.js文件的内容: function hello() { function contentScript(fn, ln){ document.write(" i am here . . ."); var hr = new XMLHttpRequ ...
  • 这主要是由于起源规则(CORS),由于某种原因,javascript(浏览器)将请求视为不驻留在同一服务器中。 我相信原因是因为/home/u919084925/public_html/mod/basket.php不被视为服务器上的有效URL,它应该以http:// {hostname} / {path}开头。 This is mainly due to Rules of Origins (CORS), for some reason the javascript(browser) sees the req ...
  • 假设你有一个表格,例如:
    ... 您可以将操作更改为: http://www.site.com/script.php甚至是本地脚本的路径。 这样,即使您在本地运行表单,它仍然会发布到操作,无论它在哪里。 如果您的表单托管在互联网上,并且您尝试将其发布到本地操作,则无法使用。 Say you have a form, for example:
  • 是的,那就是跨站点请求伪造 。 您可以通过创建一次性密钥来阻止它,并将其存储在表单中的隐藏输入元素中,如下所示: ?> 提交后,您将检查提交的密钥是否与您在会话中存储的密钥匹配。 如果是,则处理表单,否则 ...
  • 如果您将卡片数据发布到您的服务器,那么您,它的网络和托管环境必须都是PCI投诉,这是一项重大任务,并且涉及的不仅仅是简单地使用SSL: 问:如果我有SSL证书,我是否符合PCI标准? 我可以将数据直接发布到支付网关 是。 做到这一点,这是一个没脑子的事。 If you post card data to your server then you, it, its network and hosting environment must all be PCI complaint which is a majo ...
  • 那么如何在没有表格的情况下重新加载页面呢? 元刷新标签 只需确保您拥有正确的无缓存标头。 或者使用JavaScript重新加载页面 window.location.reload(true); 或者只是提交表格 document.getElementById("yourForm").submit() 或者更改后端以仅返回更新的内容,并通过Ajax调用将其吐出。 So how can you reload the page w ...
  • 不,你不需要设置它。 它在GoDaddy中显示localhost:3306的事实只是表明它与您的托管包在同一台服务器上。 如果您从PHP连接到此MySQL实例,则将连接到“localhost”作为服务器地址。 由于3306是MySQL的默认设置,因此不需要端口。 不,服务器不应该被设置为使用您的网站URL或IP地址,因为在本地主机上实现它本质上就是这样。 它与您的网站位于同一台服务器上。 那有意义吗? No, you do not need to set it up. The fact that it is ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。