Android如何为使用相机拍摄的图片添加图片。

8

我一直在开发一个相机应用,拍照和保存图片的功能都已经实现。但是我希望在拍摄照片时,能够将显示的ImageView保存在实际照片中,这个有可能吗?

目前我一直在尝试将ImageView与相机预览的布局放在同一个界面中。

<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    >

    <ImageView
        android:layout_width="50pt"
        android:layout_height="50pt"
        android:layout_gravity="center"
        android:src="@android:drawable/star_big_on"
        />

</FrameLayout>

我认为,如果在与相机相同的视图上绘制它,那么它也会将该图像与图片一起显示。

有没有人知道这是否可能,还是我需要采取其他方法?如果是这样,请提供其他解决方案的链接 :)

谢谢!

编辑:我一直在按照此相机教程http://developer.android.com/guide/topics/media/camera.html#manifest进行操作。如果有人想知道我如何保存图片等

编辑2:为了澄清,我希望在拍摄后,在所拍摄的照片中显示星星。因此,如果我拍摄一堵墙的照片,当您后来在画廊中查看照片时,我希望星星显示在那堵墙上,就像拍照时一样!


你的意思是在存储照片之前,你想用ImageView向用户显示从相机拍摄的同一张照片? - Sash_KP
不,我想要将显示在imageView中的图像与图片一起保存。因此,如果我有一个星星在中心,我希望该星星显示在图片中! - Morti
1
好的,我现在明白了并且已经为你的问题点赞了。这是一个很好的问题。我会回答它的。请给我一点时间。 - Sash_KP
请检查我的答案并让我知道是否符合您的要求。 - Sash_KP
3个回答

5
你需要的是一个LayerDrawable -
一个管理其他Drawable数组的Drawable。它们按照数组顺序绘制,因此具有最大索引的元素将绘制在顶部。
你有两种选择来使用LayerDrawable。您可以在单独的drawable xml中定义它,然后简单地设置图像在您的ImageView中,或者您可以在代码中动态配置LayerDrawable。然而,由于您希望这个过程是动态的,所以您需要下面提到的编程方法。 使用代码进行编程 您需要将您捕获的图片转换为一个Drawable,使用BitmapDrawable就可以实现:
```java Bitmap bitmap = BitmapFactory.decodeFile("your_image_path"); Drawable drawable = new BitmapDrawable(getResources(), bitmap); ```
然后,您可以创建一个LayerDrawable并将其添加到您的ImageView中,如下所示:
```java Drawable[] layers = new Drawable[2]; layers[0] = getResources().getDrawable(R.drawable.your_first_drawable); layers[1] = drawable; LayerDrawable layerDrawable = new LayerDrawable(layers); imageView.setImageDrawable(layerDrawable); ```
Resources r = getResources();
Drawable d = new BitmapDrawable(r,yourBitmap);//converting bitmap to drawable    
Drawable[] layers = new Drawable[2];
layers[0] = d;
layers[1] = r.getDrawable(R.drawable.star_big_on);
LayerDrawable layerDrawable = new LayerDrawable(layers);
imageView.setImageDrawable(layerDrawable);

现在你的ImageView上有两个图像(1.你拍摄的图像和2.star_big_on)。

编辑

如果你想要新创建的图像(带有2个添加的图像)存储在画廊中,你需要执行以下操作来存储你的图像:

int width = layerDrawable.getIntrinsicWidth();
int height = layerDrawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap); 
layerDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
layerDrawable.draw(canvas);
//and then the following line will store it in the gallery
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap , "yourTitle" , "yourDescription");

现在按照上述步骤,您的图像将被添加到相册的末尾。如果您希望它存储在相册的开头,请参考此代码
如果您想要从相册中删除在拍摄照片后存储的原始图像,您可以参考此SO问题。在这个问题中,您将看到类似于file.getPath()的内容,它就是所需删除图像的路径。因此,如果您想获取捕获图像的路径,请参考此SO问题。希望现在您已经掌握了实现您想要的一切。
注意:如果您想知道如何通过XML使用LayerDrawable

使用XML

创建一个新的Drawable XML文件,让我们称其为mylayer.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/yourcapturedimage" />
   <item android:drawable="@drawable/star_big_on" />
</layer-list>

现在在您的Activity中使用该Drawable设置图像:

imageView.setImageDrawable(getResources().getDrawable(R.layout.mylayer));

这几乎是我要找的东西。但是我不想在ImageView中显示最终图片。当我拍摄时,图片已经显示在画廊中了。我希望在拍摄时将imageView中的图片包含在图片中。 - Morti
请查看我的编辑答案。希望现在能解决你的问题。 - Sash_KP

0

我会这样做。对我来说起作用:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == REQUEST_IMAGE_CAPTURE) {
            if (resultCode == getActivity().RESULT_OK) {
                // successfully captured the image
                // display it in image view
                previewCapturedImage();
            } else if (resultCode == getActivity().RESULT_CANCELED) {
                // user cancelled Image capture
                Toast.makeText(getActivity().getApplicationContext(), "User cancelled image capture", Toast.LENGTH_SHORT).show();

            } else {
                // failed to capture image
                Toast.makeText(getActivity().getApplicationContext(),"Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void previewCapturedImage() {
        try {

            // bitmap factory
            BitmapFactory.Options options = new BitmapFactory.Options();

            // downsizing image as it throws OutOfMemory Exception for larger
            // images
            options.inSampleSize = 8;

            final Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath(),options);
            toto.setImageBitmap(bitmap);
            shareCapturedImage();
        } catch (NullPointerException e) {
            e.printStackTrace();
            cam.release();
        }
    }

    private void shareCapturedImage() {
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/*");
        share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mediaFile));
        startActivity(Intent.createChooser(share,"Share with:"));
    }

不要忘记在onCreate方法中声明ImageView,如下所示:
ImageView img = (ImageView) rootView.findViewById(R.id.img);

XML文件应该类似于这样:

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />

希望这能有所帮助 :)

如果这个回答对你有帮助,你可以给它点个赞吗? - Michele La Ferla

0
也许叠加方法可以帮助你。在捕获图像后,将其转换为位图,然后将捕获的图像和要保存的图像发送到此方法:
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(),   bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, new Matrix(), null);
    return bmOverlay;
} 

希望能解决您的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接