如何将帧添加到相机捕获的图像并保存

时间:2016-01-23 09:44:34

标签: android

我想在相机拍摄的图像上添加透明图像并将其保存到外部存储器。在此代码中我可以从相机拍摄图像并将其保存到外部存储。任何人都知道如何添加图像叠加要捕获图像并保存,请告诉我如何修改此代码。

这是我的Main3Activity.java文件

package rahula.shannirmala.com.blueandgoldperade2016;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import java.io.File;


public class Main3Activity extends AppCompatActivity {
    ImageView imageView2;
    static final int CAM_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        imageView2 = (ImageView)findViewById(R.id.imageView2);
        Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = getFile();
        camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        startActivityForResult(camera_intent,CAM_REQUEST);

    }

    private File getFile()
    {
        File folder = new File("sdcard/Blue_&_Gold_Perade");
        if (!folder.exists())
        {
            folder.mkdir();
        }

        File image_file = new File(folder,"selfie_cam.jpg");
        return image_file;

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String path = "sdcard/Blue_&_Gold_Perade/selfie_cam.jpg";
        imageView2.setImageDrawable(Drawable.createFromPath(path));
    }


}

这是我的activity_main3.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="rahula.shannirmala.com.blueandgoldperade2016.Main3Activity">

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

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView2"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
    </LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

向清单添加写入外部存储权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

活动:

public class CameraActivity extends Activity {
            private static final String LOG_TAG = "error";
            ImageView imageView;
            static final int CAM_REQUEST = 1;
            Date curentTime;

            // Storage Permissions
            private static final int REQUEST_EXTERNAL_STORAGE = 1;
            private static String[] PERMISSIONS_STORAGE = {
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            };

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_camera);

                verifyStoragePermissions(this);

                imageView = (ImageView)findViewById(R.id.imageView2);
                Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = getFile();
                camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(camera_intent,CAM_REQUEST);

            }

            private File getFile()
            {
                curentTime = new Date();
                String root = Environment.getExternalStorageDirectory().toString();
                File folder = new File(root + "/Blue_&_Gold_Perade");
                if (!folder.mkdirs()) {
                    Log.e(LOG_TAG, "Directory not created");
                }

                File image_file = new File(folder,"selfie_cam"+curentTime+".jpg");                    
                return image_file;
            }


                @Override
                protected void onActivityResult(int requestCode, int resultCode, Intent     data) {
                    setAndSaveImageWithOverlay(getBitmapOfSnappedImage());
                    }

                public Bitmap getBitmapOfSnappedImage(){


                    String root = Environment.getExternalStorageDirectory().toString();
                    String path = root + "/Blue_&_Gold_Perade"+     "/selfie_cam"+curentTime+".jpg";

                        File image = new File(path);
                        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                        Bitmap bitmap =     BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
                        return bitmap;
                    }

                public void setAndSaveImageWithOverlay(Bitmap snappedImage){
                    Bitmap b = Bitmap.createBitmap(snappedImage.getWidth(),     snappedImage.getHeight(), Bitmap.Config.ARGB_8888);

                    //the overlay png file from drawable folder
                    Bitmap overlay = BitmapFactory.decodeResource(getResources(),     R.drawable.overlay);
                    overlay =     Bitmap.createScaledBitmap(overlay,snappedImage.getWidth(),snappedImage.getHeight    (),false);

                    //create canvas with a clean bitmap
                    Canvas canvas = new Canvas(b);
                    //draw the snappedImage on the canvas
                    canvas.drawBitmap(snappedImage, 0, 0, new Paint());
                    //draw the overlay on the canvas
                    canvas.drawBitmap(overlay, 0, 0, new Paint());

                    imageView.setImageBitmap(b);

                    SaveImage(b);
                    }


                private void SaveImage(Bitmap finalBitmap) {

                    String root = Environment.getExternalStorageDirectory().toString();
                    File myDir = new File(root + "/Blue_&_Gold_Perade");
                    myDir.mkdirs();
                    String fname = "selfie_cam"+curentTime+".jpg";
                    File file = new File (myDir, fname);
                    if (file.exists ()) file.delete ();
                    try {
                        FileOutputStream out = new FileOutputStream(file);
                        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                        out.flush();
                        out.close();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                /**
                 * Checks if the app has permission to write to device storage
                 *
                 * If the app does not has permission then the user will be prompted to grant permissions
                 *
                 * @param activity
                 */
                public static void verifyStoragePermissions(Activity activity) {
                    // Check if we have write permission
                    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

                    if (permission != PackageManager.PERMISSION_GRANTED) {
                        // We don't have permission so prompt the user
                        ActivityCompat.requestPermissions(
                                activity,
                                PERMISSIONS_STORAGE,
                                REQUEST_EXTERNAL_STORAGE
                        );
                    }
                }

        }