在屏幕的一部分上使用带有摄像头的 android.view.SurfaceView [英] Using android.view.SurfaceView with a camera on part of the screen

查看:14
本文介绍了在屏幕的一部分上使用带有摄像头的 android.view.SurfaceView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图组合一个 Android 应用程序,该应用程序将拍照并以某种方式对其进行处理.我希望布局类似于 Google Goggles.意思是,顶部是相机预览,底部是一些使用纵向的控件.

I trying to put together an Android app that will take a picture and process it in some way. I'd like the layout to be similar to Google Goggles. Meaning, camera preview on the top, and some controls on the bottom using portrait orientation.

我使用这里的代码示例构建了第一个版本.这可行,但我想在底部添加一个按钮.

I've built a first version using code sample from here. This works, but I want to add a button on the bottom.

我已将 main.xml 修改为如下所示(基于这篇文章的评论):

I've modified my main.xml to look as follows (based on comments from this post):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" >

      <android.view.SurfaceView android:id="@+id/preview"
                              android:layout_width="fill_parent"
                              android:layout_height="fill_parent"
                              android:layout_alignParentTop="true" />

    <Button android:id="@+id/snap" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Snap"
            android:layout_alignParentBottom="true" />

</RelativeLayout>

但是当我运行这段代码时,我得到了以下异常:

But when I run this code I get the following exception:

E/AndroidRuntime(  199): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  199): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.beerometer/com.beerometer.ImageCapture}: android.view.InflateException: Binary XML file line #6: Error inflating class Android.view.SurfaceView
E/AndroidRuntime(  199):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime(  199):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime(  199):    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime(  199):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime(  199):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  199):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  199):    at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(  199):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  199):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  199):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(  199):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(  199):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  199): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class Android.view.SurfaceView
E/AndroidRuntime(  199):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
E/AndroidRuntime(  199):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
E/AndroidRuntime(  199):    at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
E/AndroidRuntime(  199):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime(  199):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
E/AndroidRuntime(  199):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
E/AndroidRuntime(  199):    at android.app.Activity.setContentView(Activity.java:1622)
E/AndroidRuntime(  199):    at com.beerometer.ImageCapture.onCreate(ImageCapture.java:37)
E/AndroidRuntime(  199):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  199):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
E/AndroidRuntime(  199):    ... 11 more
E/AndroidRuntime(  199): Caused by: java.lang.ClassNotFoundException: Android.view.SurfaceView in loader dalvik.system.PathClassLoader@44bfc7b0
E/AndroidRuntime(  199):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
E/AndroidRuntime(  199):    at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
E/AndroidRuntime(  199):    at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
E/AndroidRuntime(  199):    at android.view.LayoutInflater.createView(LayoutInflater.java:466)
E/AndroidRuntime(  199):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
E/AndroidRuntime(  199):    ... 20 more

当我用其他东西替换上面的 SurfaceView 时,例如一个 TextView,它显示,但在横向.

When I replace the SurfaceView above with something else, e.g. a TextView, that it displays, but in landscape.

如何使用纵向视图在部分屏幕上获得相机预览?

How can I get a camera preview on part of the screen using portrait view?

谢谢

推荐答案

首先,您的布局可能无法满足您的需求.考虑使用 RelativeLayoutButton 锚定在底部,SurfaceView 锚定在屏幕顶部,<代码>按钮.

First, your layout probably won't give you what you want. Consider using a RelativeLayout, with the Button anchored to the bottom, and the SurfaceView anchored to the top of the screen and the top of the Button.

此外,您的 SurfaceView 上有一个重复的 xmlns:android="http://schemas.android.com/apk/res/android"不需要.根元素上的那个就足够了.

Also, you have a duplicate xmlns:android="http://schemas.android.com/apk/res/android" on your SurfaceView that you don't need. The one on your root element will suffice.

就您的异常而言,您的堆栈跟踪中可能缺少更多详细信息,解释了 startPreview() 失败的原因.在堆栈跟踪中查找Caused by"或其他行.如果您无法识别它,请编辑您的问题并粘贴到整个堆栈跟踪中(并通过对此答案的评论来 ping 我,因为否则我不会知道编辑).

In terms of your exception, there may be more detail in your stack trace that you are missing, explaining why startPreview() failed. Look for a "Caused by" or other line mid-way through the stack trace. If you can't identify it, edit your question and paste in the entire stack trace (and ping me via a comment on this answer, since I won't know about the edit otherwise).

您也可以尝试使用 本书示例另一个 Camera 预览应用,看看我解决问题的方法是否更适合您的情况.

You might also experiment with this book example as another Camera preview app, to see if there's something about how I approach the problem that works better for your circumstance.

这篇关于在屏幕的一部分上使用带有摄像头的 android.view.SurfaceView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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