相机预览:没有错误,但没有图片:-(

发布于 2024-10-14 20:25:14 字数 2298 浏览 2 评论 0原文

已阅读书籍和提示,消除所有编译错误和警告并放入一些调试语句。

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 调试器中,我收到一条“创建活动”的日志消息,后跟“现在等待一些回调”,而在我的调试消息方面仅此而已,所以我认为回调不起作用 - 我一生都不能看看我哪里错了。

在清单中我有

Activity 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 Wildfire)上,页面加载,textView 消息出现在左上角,仅此而已。

应该提到的是,我对这个平台非常陌生,并且承认我可能错过了一些非常非常基本的东西。

任何想法/评论将非常感激,

奥利弗

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

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.

In the manifest I have

The Activity XML page has

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

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.

Any ideas/comments will be very much appreciated,

Oliver

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上得阳光 2024-10-21 20:25:14

看一下 ApiDemos(图形 -> CameraPreview)和 CameraPreview 类。这就是我所做的(顺便说一句,所有的 ApiDemo 都很棒),而且效果非常好。最好先让某些东西发挥作用 - 然后您可以将其从应用程序中不需要的东西中删除。该演示也在线: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html。希望有帮助。

take a look at the ApiDemos (Graphics->CameraPreview) and the class CameraPreview. That was what I did (all the ApiDemos are great btw) and it worked like a charm. It's good to have something working first - you can then strip it off the stuff you don't need in your app. The demo is also online here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html. Hope that helped.

梦与时光遇 2024-10-21 20:25:14

没有足够的时间来解释(几分钟后与朋友见面)。我基本上在“surfaceChanged()”中添加了一些函数(这是您应该开始预览的地方)。

因此,您不需要在“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 文件中使用 mCam.startPreview() (您做了一些复制和粘贴错误)

<?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"
    />

希望我能提供帮助:)
我认为,通过设置方向,您可以将相机图片更改为纵向模式。 :)(现在是风景)

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).

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-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)

旧城空念 2024-10-21 20:25:14

老实说,我认为“手动”处理整个相机预览过于复杂。
如果您只想先拍照并预览,您可以通过 Intent 告诉系统。我前段时间写过一篇关于此的文章 http://javablogs.com/Jump.action? id=618025
基本上是这样的:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);

然后在您的活动中,您可以在 onActivityResult() 回调中获取数据

@Override
public void onActivityResult(int requestCode, int resultCode,Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if(requestCode==1&& resultCode==RESULT_OK) {
    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
  }
}

如果您需要比默认值更大的图像,请查看 MediaStore.EXTRA_OUTPUT

To be honest I think that whole camera preview handling "by hand" is overly complex.
If you just want to take a picture and have a preview first, you can tell this the system via an Intent. I've written a post about this some time ago http://javablogs.com/Jump.action?id=618025
Basically it goes:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);

And then within your activity you can get the data in a onActivityResult() callback

@Override
public void onActivityResult(int requestCode, int resultCode,Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if(requestCode==1&& resultCode==RESULT_OK) {
    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
  }
}

If you need a larger image than the default, have a look at MediaStore.EXTRA_OUTPUT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文