Android 相机预览与文本视图

发布于 2024-11-13 07:41:53 字数 3618 浏览 3 评论 0原文

我的 Android 应用程序上有相机预览,但是当我向 xml 中的 TextView 添加“id”时,该应用程序不再运行。你不允许有一个带id的textview吗?我需要这个,因为我希望在进行相机预览时更改屏幕上的文本?

main.xml

<?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"
 > 
<SurfaceView   android:id="@+id/camerapreview"  
android:layout_width="fill_parent"  
android:layout_height="wrap_content"  
/> 
</LinearLayout>

control.xml

<?xml version="1.0" encoding="utf-8"?>

    <ImageView android:src="@drawable/icon" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_gravity="center"
        android:paddingTop="120dp" />
    <TextView android:layout_width="wrap_content" android:id="@+id/mind" 
        android:layout_height="wrap_content" android:text="First Text"
        android:layout_gravity="center" android:paddingTop="50dp"
        android:textSize="30dp" android:textStyle="bold" android:textColor="#FFFFFF" />
    <Button android:id="@+id/takepicture" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Save Image"
        android:layout_gravity="center" android:paddingTop="10dp"/>
</LinearLayout>

main.java:

package com.example.androidcamera;

import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

public class main extends Activity implements SurfaceHolder.Callback {

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.control, null);
    LayoutParams layoutParamsControl = new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);

    TextView textGenerateDesc = (TextView) findViewById(R.id.mind);
    textGenerateDesc.setText("hi");

    Button buttonTakePicture = (Button) findViewById(R.id.takepicture);
    buttonTakePicture.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            camera.takePicture(myShutterCallback,    myPictureCallback_RAW,
                    myPictureCallback_JPG);
        }
    });
}

I have camera preview on my android app but when I add an 'id' to the TextView in the xml then the app no longer runs. Are you not allowed to have a textview with an id? I need this because I would like the text to change on screen while having the camera preview?

main.xml

<?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"
 > 
<SurfaceView   android:id="@+id/camerapreview"  
android:layout_width="fill_parent"  
android:layout_height="wrap_content"  
/> 
</LinearLayout>

control.xml

<?xml version="1.0" encoding="utf-8"?>

    <ImageView android:src="@drawable/icon" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_gravity="center"
        android:paddingTop="120dp" />
    <TextView android:layout_width="wrap_content" android:id="@+id/mind" 
        android:layout_height="wrap_content" android:text="First Text"
        android:layout_gravity="center" android:paddingTop="50dp"
        android:textSize="30dp" android:textStyle="bold" android:textColor="#FFFFFF" />
    <Button android:id="@+id/takepicture" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Save Image"
        android:layout_gravity="center" android:paddingTop="10dp"/>
</LinearLayout>

main.java:

package com.example.androidcamera;

import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

public class main extends Activity implements SurfaceHolder.Callback {

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.control, null);
    LayoutParams layoutParamsControl = new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);

    TextView textGenerateDesc = (TextView) findViewById(R.id.mind);
    textGenerateDesc.setText("hi");

    Button buttonTakePicture = (Button) findViewById(R.id.takepicture);
    buttonTakePicture.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            camera.takePicture(myShutterCallback,    myPictureCallback_RAW,
                    myPictureCallback_JPG);
        }
    });
}

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

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

发布评论

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

评论(2

§对你不离不弃 2024-11-20 07:41:53

正如我在 xml 中看到的,您将 textview id 指定为“textview”,在代码中您通过 R.id.mind 获取它,

您是否更改了此处的代码或其你有什么代码?

as I can see in the xml you give the textview id as "textview" and in code you take it by R.id.mind

Did you changed the code here or its the code that you have?

困倦 2024-11-20 07:41:53

您刚刚错过了“LinearLayout”的起始标签,

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

其余部分是相同的

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="120dp"
 />
<TextView
android:id="@+id/mind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="50dp"
android:text="First Text"
android:textColor="#FFFFFF"
android:textSize="30dp"
android:textStyle="bold" />

<Button
android:id="@+id/takepicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="10dp"
android:text="Save Image" />

</LinearLayout>

You just missed the starting tag of 'LinearLayout'

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

rest are same

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="120dp"
 />
<TextView
android:id="@+id/mind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="50dp"
android:text="First Text"
android:textColor="#FFFFFF"
android:textSize="30dp"
android:textStyle="bold" />

<Button
android:id="@+id/takepicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="10dp"
android:text="Save Image" />

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