相机预览:没有错误,但没有图片:-(
已阅读书籍和提示,消除所有编译错误和警告并放入一些调试语句。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下 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.
没有足够的时间来解释(几分钟后与朋友见面)。我基本上在“surfaceChanged()”中添加了一些函数(这是您应该开始预览的地方)。
因此,您不需要在“surfaceCreated()”
xml 文件中使用 mCam.startPreview() (您做了一些复制和粘贴错误)
希望我能提供帮助:)
我认为,通过设置方向,您可以将相机图片更改为纵向模式。 :)(现在是风景)
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()"
xml-file (you did a little copy & paste error)
Hope I could help :)
I think, via setting the orientation, you could change the camera picture to portrait mode. :) (it's landscape right now)
老实说,我认为“手动”处理整个相机预览过于复杂。
如果您只想先拍照并预览,您可以通过 Intent 告诉系统。我前段时间写过一篇关于此的文章 http://javablogs.com/Jump.action? id=618025
基本上是这样的:
然后在您的活动中,您可以在
onActivityResult()
回调中获取数据如果您需要比默认值更大的图像,请查看 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:
And then within your activity you can get the data in a
onActivityResult()
callbackIf you need a larger image than the default, have a look at MediaStore.EXTRA_OUTPUT.