【问题标题】:Android: Saving photo with image on photoAndroid:在照片上保存带有图像的照片
【发布时间】:2013-02-05 16:35:42
【问题描述】:

当我用我的 Android 相机拍照时,我的屏幕上有一张图像 (imageview3) 我想和我的照片一起保存。

这是我的onPictureTaken 方法

public void onPictureTaken(byte[] data, Camera camera) {
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/Ker");
    imagesFolder.mkdirs();
    String fileName = "Ker_.jpg";
    output = new File(imagesFolder, fileName);
    ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3);
    view.setDrawingCacheEnabled(true);
    Bitmap b = view.getDrawingCache();
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(output);
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
    b.compress(CompressFormat.JPEG, 95, fos);
    try {
        fos.write(data);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
        catch (IOException e) {
        e.printStackTrace();
    }
    camera.stopPreview();
}

当我打开文件夹时,保存的图片只有黑色背景的imageview3。为什么没有保存真实的相机视图?

编辑 我也在尝试画布:

output = new File(imagesFolder, fileName);
            ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3);
            view.setDrawingCacheEnabled(true);
            Bitmap b = view.getDrawingCache();   
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(output);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
              catch (IOException e) {
                    e.printStackTrace();
            }
            FileOutputStream fos2 = null;
            b.compress(CompressFormat.JPEG, 95, fos2);

            try {
                Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fos.getFD());
                Bitmap bitmap2 = BitmapFactory.decodeFileDescriptor(fos2.getFD());
                Canvas canvas = new Canvas(bitmap);
                canvas.drawBitmap(bitmap2, null, null);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

正确吗?如何将画布保存到我的 sdcard 上的文件中(即将画布中的数据写入文件输出流)

【问题讨论】:

    标签: android camera image


    【解决方案1】:

    您将图像视图的 JPEG 和相机中的 JPEG 附加到一个文件中。

    为每个文件创建一个单独的文件输出流,并将来自 Bitmap.compress() 和 onPictureTaken 数据数组的数据写入各自的流中。

    如果您想将两张图片合并为一张图片,您需要将数据数组解码为Bitmap,然后使用Canvas将ImageView位图和相机捕获的Bitmap绘制到画布上您想要的安排,然后将其保存为单个 jpeg。

    您不能简单地将两个压缩的 JPEG 比特流连接在一起;文件格式不能那样工作。

    【讨论】:

    猜你喜欢
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 2012-01-25
    • 1970-01-01
    • 2017-03-18
    相关资源
    最近更新 更多