将图像保存为两个图层的组合

时间:2014-04-20 15:11:53

标签: android camera imageview frame surfaceview

我正在尝试使用自定义框架制作相机应用。我的布局如下......

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <!-- Place the objects you want on the bottom here -->

    <SurfaceView
        android:id="@+id/surface01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- Objects that should be on top of the SurfaceView -->

    <ImageView
        android:id="@+id/pictureFrame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/frame" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/controls"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerVertical="true"
        android:orientation="horizontal"
        android:weightSum="5" >
        <LinearLayout
            android:layout_width="0dp"
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>

        <Button
            android:id="@+id/btnFrame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Change Frame" />

        <Button
            android:id="@+id/btnSnap"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Take Photo" />
    </LinearLayout>
</RelativeLayout>

我的MainActivity.java代码如下所示......

public class MainActivity extends Activity implements SurfaceHolder.Callback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        snap = (Button) findViewById(R.id.btnSnap);
        snap.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO take a photo, combine the Frame Layer with the camera
                // preview to produce a single jpeg. Once a photo has been
                // taken, show it to the user and give them sharing options
                camera.takePicture(myShutterCallback,  myPictureCallback_RAW, myPictureCallback_JPG);

            }
        });

    }
    ShutterCallback myShutterCallback = new ShutterCallback(){

        @Override
        public void onShutter() {
            // TODO Auto-generated method stub

        }

    };
    PictureCallback myPictureCallback_RAW = new PictureCallback(){

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            // TODO Auto-generated method stub

        }

    };
    PictureCallback myPictureCallback_JPG = new PictureCallback(){

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Bitmap bitmapPicture=BitmapFactory.decodeByteArray(data, 0, data.length);
            Uri uriTarget= getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
            OutputStream imageFileOS;
            try{
                imageFileOS=getContentResolver().openOutputStream(uriTarget);
                imageFileOS.write(data);
                imageFileOS.flush();
                imageFileOS.close();
                Toast.makeText(getApplicationContext(),
                        "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();

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera=null;
        previewing=false;
    }
}

目前我只能保存没有框架的surfaceview中的内容。是否可以将图像保存为SurfaceView和ImageView的组合?任何帮助将不胜感激。

0 个答案:

没有答案