简体   繁体   English

如何在相机拍摄的图像上添加相框并保存

[英]how to add a frame to camera captured image and save it

I want to add a transparent image over the camera captured image and save it to the external storage.in this code i can take the image from camera and save it to external storage.is anyone know that how to add a image overlay to captured image and save it,please tell me how to modify this code. 我想在相机捕获的图像上添加透明图像并将其保存到外部存储中。在此代码中,我可以从相机中捕获图像并将其保存到外部存储中。有人知道如何向捕获的图像添加图像叠加层吗?并保存,请告诉我如何修改此代码。

This is my Main3Activity.java file 这是我的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));
    }


}

This is my activity_main3.xml file 这是我的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>

Add write external storage permission to the manifest: 向清单添加写入外部存储权限:

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

The activity: 活动:

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
                        );
                    }
                }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将相机拍摄的图像保存在特定文件夹中 - How to save image captured with camera in specific folder 如何在 android 中保存从相机拍摄的图像 - How to save captured image from camera in android 如何对相机拍摄的图像添加不同的效果 - How to add different effects on image captured by camera 保存相机拍摄的图像JPG - Save image JPG captured by the camera 如何在Android中使用自定义文件名保存相机拍摄的图像? - How to save camera captured image with custom file name in Android? 保存图像覆盖与相机捕获图像underneith - Save image overlay with camera captured image underneith 如何绘制摄像机视图的图像框并将图像框重叠在捕获的图像上 - how to draw image frame for camera view and overlapping the image frame on captured image 将相机捕获的图像保存在android的外部存储中 - save image captured by camera in external storage in android 如何从相机预览(表面视图)中裁剪图像并保存它,而不是像instagram app这样捕获bt相机的实际图像? - How to crop image from camera preview (surface view) and save it instead of actual image captured bt camera like instagram app? 如何将相机拍摄的图像保存在手机内存中以检查文件大小? - How to save the captured image from camera in the phone memory to check the file size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM