java - Android自定义相机预览?

标签 java android camera android-camera

我正在创建一个camera申请android 。 我想要我的camera preview如下图所示的 Activity (例如)。我想分我的camera preview Activity 分为四个或多个部分。

也就是说,当我点击 camera 中的图片后,当我回到我的(相机图像预览 Activity )activity ,我想要 Activity screen分为四个或更多部分,每个部分都应该有 preview image就在上面。

原因是我想用图像 render-script 来试验每个预览。效果[但该解决方案超出了我的问题范围,因为我知道如何使用渲染脚本],并且每个部分都应该有单独的 rendering .

有人可以帮我找到解决方案吗?

Four image previews instead of one, after taking picture from camera

如果有任何示例或教程,这将是一个很大的帮助。

提前致谢。

最佳答案

经过一番努力,我找到了解决办法。是啊啊啊!!!

相机.xml:

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

    <FrameLayout 
        android:id="@+id/preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    </FrameLayout>
    <LinearLayout
        android:id="@+id/imageViewLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

                <ImageView
                    android:id="@+id/img1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

                <ImageView
                    android:id="@+id/img2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="@drawable/ic_launcher" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

                <ImageView
                    android:id="@+id/img3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

                <ImageView
                    android:id="@+id/img4"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="@drawable/ic_launcher" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/clickButton"
        style="@android:style/Animation.Translucent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/click" />

</RelativeLayout>

您的相机 Activity 应该是这样的:我只发布我认为您应该关注的主要部分,其余部分您可以实现。如果你卡在某个地方请问我。 首先,您应该从您的 Activity 中调用:

final Button clickButton = (Button) findViewById(R.id.clickButton);
            clickButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(final View view) {
                    camera.takePicture(null, null, mPicture);
                }
            });

你的 onPictureTaken() 应该是这样的:

/**
     * This will be called after taking picture from camera.
     */
    final transient private PictureCallback mPicture = new PictureCallback() {
        /**
         * After taking picture, onPictureTaken() will be called where image
         * will be saved.
         * 
         */
        @Override
        public void onPictureTaken(final byte[] data, final Camera camera) {
            OutputStream outStream = null;
            try {
                outStream = new BufferedOutputStream(new FileOutputStream(
                        mGetFile));
                outStream.write(data); // Write the data to corresponding place.
                ((FrameLayout) findViewById(R.id.preview))
                        .setVisibility(View.GONE);
                ((LinearLayout) findViewById(R.id.imageViewLayout))
                        .setVisibility(View.VISIBLE);
                final Options options = new Options();
                options.inScaled = true;
                options.inPurgeable = true;

                // to scale the image to 1/8
                options.inSampleSize = 8;

                Bitmap imageBitmap = BitmapFactory.decodeByteArray(data, 0,
                        data.length, options);
                ImageView image1 = (ImageView) findViewById(R.id.img1);
                ImageView image2 = (ImageView) findViewById(R.id.img2);
                ImageView image3 = (ImageView) findViewById(R.id.img3);
                ImageView image4 = (ImageView) findViewById(R.id.img4);

                image1.setImageBitmap(imageBitmap);
                image2.setImageBitmap(imageBitmap);
                image3.setImageBitmap(imageBitmap);
                image4.setImageBitmap(imageBitmap);
            } catch (FileNotFoundException e) {
                camera.release();
            } catch (IOException e) {
                camera.release();
            } finally {
                try {
                    outStream.close();

                } catch (IOException e) {
                    camera.release();
                } 
            }
        }
    };

这将显示捕获的图像,如您上面提到的图像。

谢谢:)

关于java - Android自定义相机预览?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10827689/

相关文章:

java - Aptana Studio 3 启动错误 - 无法加载 JNI 共享库 "YOUR SYSTEM PATH OF JAVA...bin\server\jvm.dll"

java - 从 application.yml 读取配置到 POJO 列表的 Map

java - 需要帮助解决 ExpandableListView 中的 IndexOutOfBoundsException

Android Dagger 2 与 BaseActivity 可减少样板文件

opencv - 光圈的变化会影响相机校准吗?

不同颜色棋盘的 OpenCV 相机标定

java - 如何在谷歌电子表格 API 中复制工作表

java - 属于动态组的数据库良好实践 : how to make users,

android - 改造 2 后 rxjava(登录)

c++ - 使用 DMA 仅从 DCMI 读取每秒的字节