【问题标题】:Surface view draw element of layout (android)布局的表面视图绘制元素(android)
【发布时间】:2012-06-30 18:17:05
【问题描述】:

我有一个表面视图,我希望在我的表面视图上“绘制”线性布局的内容。

我的表面视图是相机的预览,我希望当我拍照时我的布局内容被绘制在图片上。

这是怎么做到的?

谢谢。

【问题讨论】:

    标签: java android camera surfaceview


    【解决方案1】:

    表面布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:gravity="bottom"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    
    <ImageView
            android:id="@+id/takepicture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/trans_tshirt"
            android:contentDescription="tshirt" />
    

    main.xml

    <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
            <SurfaceView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/camerapreview"/>
    </LinearLayout>
    
    
    java code:
    
    package com.views;
    
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.pm.ActivityInfo;
    import android.graphics.PixelFormat;
    import android.hardware.Camera;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.LayoutInflater;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.ImageView;
    import android.widget.Toast;
    import com.fashion.alex.R;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.OutputStream;
    
    public class HomeActivity extends Activity implements SurfaceHolder.Callback
    {
    
        private Camera camera;
        private SurfaceView surfaceView;
        private SurfaceHolder surfaceHolder;
        private boolean previewing = false;
        private LayoutInflater controlInflater = null;
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
            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.tshirt_layout, null);
            LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            this.addContentView(viewControl, layoutParamsControl);
            final ImageView ivButton = (ImageView)findViewById(R.id.takepicture);
            ivButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    camera.takePicture(myShutterCallback,
                            myPictureCallback_RAW, myPictureCallback_JPG);
                }}
            );
        }
    
        Camera.ShutterCallback myShutterCallback = new Camera.ShutterCallback(){
            @Override
            public void onShutter() {
    
            }
        };
    
        Camera.PictureCallback myPictureCallback_RAW = new Camera.PictureCallback(){
            @Override
            public void onPictureTaken(byte[] arg0, Camera arg1) {
            }
        };
        Camera.PictureCallback myPictureCallback_JPG = new Camera.PictureCallback(){
            @Override
            public void onPictureTaken(byte[] arg0, Camera arg1) {
                Uri uriTarget = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
                OutputStream imageFileOS;
                try {
                    imageFileOS = getContentResolver().openOutputStream(uriTarget);
                    imageFileOS.write(arg0);
                    imageFileOS.flush();
                    imageFileOS.close();
                    Toast.makeText(HomeActivity.this,
                            "Image saved: " + uriTarget.toString(),
                            Toast.LENGTH_LONG).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
                camera.startPreview();
            }
        };
    
    
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            if(previewing){
                camera.stopPreview();
                previewing = false;
            }
            if (camera != null){
                try {
                    camera.setPreviewDisplay(surfaceHolder);
                    camera.startPreview();
                    previewing = true;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            camera = Camera.open();
            try {
                camera.setPreviewDisplay(holder);
            } catch (IOException exception) {
                camera.release();
                camera = null;
            }
        }
    
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            camera.stopPreview();
            camera.release();
            camera = null;
            previewing = false;
        }
    }
    

    【讨论】:

    • @lamStalker:感谢您的代码,它工作正常,但是当我使用此代码时,相机预览显示为 90 度。
    【解决方案2】:

    创建一个 Layout/.xml 文件..如下所示

    1) 线性布局 A 2) 线性布局 A 必须有子视图,可以是任何你想要的按钮/文本。

    3) 线性布局 A 还必须有一个surfaceview B

    在您的活动中使用此surfaceView B 来显示相机预览。这样你基本上可以在屏幕上有表面视图和其他视图。

    【讨论】:

    • 是的,我做到了。但是我想当我得到我的表面视图的图片时,我的布局元素被绘制在我的图片中。有可能吗?
    猜你喜欢
    • 1970-01-01
    • 2012-06-25
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多