相机preVIEW:没有错误,但没有图像:-( [英] Camera Preview: No errors but no picture :-(

查看:501
本文介绍了相机preVIEW:没有错误,但没有图像:-(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曾经读过的书和提示,摆脱了所有的编译错误和警告,并把一些调试语句。

Have read the books and hints, got rid of all compile errors and warnings and put in some debug statements.

package com.cit.BroadcastSim;

import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class BroadcastActivity extends Activity implements SurfaceHolder.Callback {
  public Camera myCamera;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.broadcast);  // Inflate the broadcast.xml file

    Log.d("BROADCAST", "Creating the Activity");
    SurfaceView cameraSurface = (SurfaceView)findViewById(R.id.camerasurface);
    SurfaceHolder cameraHolder = cameraSurface.getHolder();
    cameraHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    cameraHolder.addCallback(this);
    Log.d("BROADCAST", "Now wait for some CallBacks");
}


  public void surfaceCreated(SurfaceHolder holder) {
    // Surface created, now it is possible to set the preview
    Log.d("BROADCAST", "Surface Created");
    try {
      Log.d("BROADCAST","CAMERA: NOT NULL");
      myCamera = Camera.open();
      myCamera.setPreviewDisplay(holder);
      myCamera.startPreview();
      } catch (IOException e) {
        Log.d("BROADCAST", e.getMessage());
      }
  }

  public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d("BROADCAST", "Surface Destroyed");
    myCamera.stopPreview();
    myCamera.release();
  }

  public void surfaceChanged(SurfaceHolder holder, int I, int J, int K) {
    Log.d("BROADCAST", "Surface Changed");
    myCamera.stopPreview();
    myCamera.release();
  }
}

在DDMS调试器,我得到了'创建活动其次是现在等待一段回调',仅此而已,我调试消息方面日志消息,所以我认为回调不工作 - 为我的生活不能看到我弄错了。

In the DDMS debugger, I get a log message for 'Creating the Activity' followed by 'Now wait for some CallBacks' and nothing more in terms of my Debug messages so I think Callback is not working - for the life of me can't see where I have got it wrong.

在清单我有


 
 

在活动XML页面有

<TextView  
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/Broadcast"
  />
<SurfaceView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/camerasurface" 
  />
</LinearLayout>

最后,Android手机(HTC野火),页面加载,将显示在左上角的TextView的消息,仅此而已。

Finally, on the Android phone (HTC Wildfire), the page loads, the textView message appears at the top left and that is all.

应该提到,我很新的这个平台和接受,我可能会错过一些非常非常基本的。

Should mention that I am very new to this platform and accept that I might have missed something very very basic.

任何想法/意见将是非常美联社preciated,

Any ideas/comments will be very much appreciated,

奥利弗

推荐答案

没有足够的时间来解释(与朋友会议上几分钟)。我基本上都添加了一些功能整合到surfaceChanged()(这是你应该开始preVIEW)。

haven't got enough time to explain (meeting with friends in a few minutes). I basically added some functions into "surfaceChanged()" (this is where you should start the preview).

因此​​,你不需要mCam.start preVIEW()在surfaceCreated()

Therefore you don't need mCam.startPreview() in "surfaceCreated()"

    public void surfaceCreated(SurfaceHolder holder) {
    // Surface created, now it is possible to set the preview
    Log.d("BROADCAST", "Surface Created");
    try {
      Log.d("BROADCAST","CAMERA: NOT NULL");
      mCam = Camera.open();
      mCam.setPreviewDisplay(holder);
      //myCamera.startPreview();
      } catch (IOException e) {
        Log.d("BROADCAST", e.getMessage());
      }
  }

  public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d("BROADCAST", "Surface Destroyed");
    mCam.stopPreview();
    mCam.release();
  }

  public void surfaceChanged(SurfaceHolder holder, int I, int J, int K) {
      Camera.Parameters parameters = mCam.getParameters();
    List<Size> previewSizes = parameters.getSupportedPreviewSizes();
    Size bestSize = null;
    int bestDiff = 0;
    int diff = 0;
    for (Size size : previewSizes) {
            diff = Math.abs(K - size.height) + Math.abs(J - size.width);
        if (bestSize == null || diff < bestDiff) {
                bestSize = size;
                bestDiff = diff;
        }
        parameters.setPreviewSize(bestSize.width, bestSize.height);
        mCam.setParameters(parameters);
    }

    //start preview of camera
      mCam.startPreview();
  }

XML文件(您做了一个小小的复制和放大器;粘贴错误)

xml-file (you did a little copy & paste error)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

希望我能帮助:)
我认为,通过设置方向,你可以改变摄像机画面到肖像模式。 :)(它的景观现在)
    

Hope I could help :) I think, via setting the orientation, you could change the camera picture to portrait mode. :) (it's landscape right now)

这篇关于相机preVIEW:没有错误,但没有图像:-(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆