【问题标题】:Take picture from Camera Intent and overlay another small image on it从相机意图中拍照并在其上叠加另一个小图像
【发布时间】:2013-01-28 19:08:54
【问题描述】:

我有一个应用程序,现在只需单击一个按钮即可打开相机。然后用户可以使用标准相机应用程序拍照,这会导致取消/保存选项。如果选择取消,则可以重新拍摄照片。如果选择了保存选项,图像将保存到图库。在单击之前,我想在此取消/保存模式下向图像添加一些内容。

我认为最好的方法是将照片带入我的应用程序并在那里进行修改并使用按钮保存。我不知道该怎么做。我知道我必须使用 onActivityResult 函数,但仅此而已。

感谢任何建议。

【问题讨论】:

    标签: java android image-processing bitmap camera


    【解决方案1】:

    当您启动 IMAGE_CAPTURE 意图让用户拍照时,您应该将存储图像的路径作为参数传递。 首先你应该保存拍摄图片的路径,然后,当用户回到你的活动时,管理位图并与其他元素组合。

      camera.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        String storageState = Environment.getExternalStorageState();
                        if (storageState.equals(Environment.MEDIA_MOUNTED)) {
                            long time = System.currentTimeMillis();
    
                            File root = Environment
                                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    
                            File dir = new File(root.getAbsolutePath() + "/Camera");
                            if (dir.exists() == false) {
                                dir.mkdirs();
                            }
    
                            String path = dir.getAbsolutePath() + File.separatorChar
                                    + time + ".jpg";
                            filesaved = new File(path);
                            try {
                                if (filesaved.exists() == false) {
                                    filesaved.getParentFile().mkdirs();
                                    filesaved.createNewFile();
                                }
    
                            } catch (IOException e) {
                                Toast.makeText(
                                        context,
                                        "Unable to create external file"
                                                + storageState, Toast.LENGTH_LONG).show();
                                return;
                            }
    
    
                            uritopass = Uri.fromFile(filesaved);
                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, uritopass);
                            startActivityForResult(intent, TAKE_PICTURE);
                        } else {
                            Toast.makeText(
                                    context,
                                    "External Storeage (SD Card) is required.\n\nCurrent state: "
                                            + storageState, Toast.LENGTH_LONG).show();
    
                        }
                    }
    
                });
    
        ...
    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == Activity.RESULT_OK) {
                if (requestCode == TAKE_PICTURE) {
                    //user took a photo
                    File imageFile = new File(filesaved.toString());
                    Bitmap bm = decodeFile(imageFile);
                    if (bm != null) {
                        bm = combineImages(bm);
                        img.setImageBitmap(bm);
                    }
                }
            }
    }
    
    ...
    

    decodefile方法从原始文件Here加载位图。

    combineImages 组合2个或多个位图Here的方法。

    【讨论】:

    猜你喜欢
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多