-1

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

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

这是怎么做的?

谢谢。

4

2 回答 2

3

表面布局:

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

主要的.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;
    }
}
于 2012-06-30T19:07:00.500 回答
0

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

1)LinearLayout A 2)LinearLayout A 必须有子视图,可以是按钮/文本,无论你想要什么。

3)Linear Layout A也必须有surfaceview B

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

于 2012-06-30T18:37:34.907 回答