保存相机preVIEW与叠加图像 [英] Save cameraPreview with overlay image

查看:275
本文介绍了保存相机preVIEW与叠加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用谷歌的相机preVIEW样品演示。在这里,我有相机preVIEW,并在摄像头preVIEW的叠加图像。

当我拍摄照片时,只有相机preVIEW是保住了,但我需要的叠加图像将超过相机preVIEW也得救了。

这是我的方式保存相机preVIEW的照片,但我需要知道什么是对覆盖展位图像的最佳方式,并将它们保存到一个。

 公共无效onPictureTaken(字节[]数据,相机摄像头){    。字符串photoPath = Environment.getExternalStorageDirectory()getAbsolutePath()+/ miFoto.jpg;    尝试{
        FOS的FileOutputStream =新的FileOutputStream(photoPath);
        fos.write(数据);
        fos.close();
    }赶上(IOException异常五){
        Toast.makeText(这一点,产品图未保存,Toast.LENGTH_SHORT).show();
        返回;
    }
    Toast.makeText(这一点,产品图保存在:+ photoPath,Toast.LENGTH_SHORT).show();    camera.start preVIEW();
}


解决方案

我终于做到了这种方式:

 公共无效onPictureTaken(字节[]数据,相机摄像头){    位图cameraBitmap = BitmapFactory.de codeByteArray的(数据,0,data.length);
    位图cameraScaledBitmap = Bitmap.createScaledBitmap(cameraBitmap,1280,720,真正的);
    INT妇女参与发展= cameraScaledBitmap.getWidth();
    INT HGT = cameraScaledBitmap.getHeight();
    位图newImage = Bitmap.createBitmap(WID,HGT,Bitmap.Config.ARGB_8888);
    位图overlayScaledBitmap = Bitmap.createScaledBitmap(overlayBit​​map,妇女参与发展,HGT,真正的);
    帆布帆布=新的Canvas(newImage);
    canvas.drawBitmap(cameraScaledBitmap,0,0,NULL);
    canvas.drawBitmap(overlayScaledBitmap,0,0,NULL);    文件storagePath =新的文件(Environment.getExternalStorageDirectory()getAbsolutePath());
    storagePath.mkdirs();
    串finalName = Long.toString(System.currentTimeMillis的());
    文件MYIMAGE =新的文件(storagePath,finalName +.JPG);    。字符串photoPath = Environment.getExternalStorageDirectory()getAbsolutePath()+/+ finalName +.JPG;    尝试{
        FOS的FileOutputStream =新的FileOutputStream(MYIMAGE);
        newImage.com preSS(Bitmap.Com pressFormat.JPEG,80,FOS);
        fos.close();
    }赶上(IOException异常五){
        Toast.makeText(这一点,产品图未保存,Toast.LENGTH_SHORT).show();
        返回;
    }
    Toast.makeText(这一点,产品图保存在:+ photoPath,Toast.LENGTH_SHORT).show();    camera.start preVIEW();
    newImage.recycle();
    newImage = NULL;
    cameraBitmap.recycle();
    cameraBitmap = NULL;
}

I'm using Google's CameraPreview sample demo. Here, I have the cameraPreview, and an overlayed image over cameraPreview.

When I take the photo, only the cameraPreview is saved, but I need the overlay image to be saved too over the cameraPreview.

This is the way I save cameraPreview's photo, but I need to know what would be the best way for overlaying booth images and save them into one.

public void onPictureTaken(byte[] data, Camera camera) {

    String photoPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/miFoto.jpg";

    try {
        FileOutputStream fos = new FileOutputStream(photoPath);
        fos.write(data);
        fos.close();
    } catch (IOException e) {
        Toast.makeText(this, "Pic not saved", Toast.LENGTH_SHORT).show();
        return;
    }


    Toast.makeText(this, "Pic saved in: " + photoPath, Toast.LENGTH_SHORT).show();

    camera.startPreview();
}

解决方案

I finally made it this way:

public void onPictureTaken(byte[] data, Camera camera) {

    Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    Bitmap cameraScaledBitmap = Bitmap.createScaledBitmap(cameraBitmap, 1280, 720, true);
    int wid = cameraScaledBitmap.getWidth();
    int hgt = cameraScaledBitmap.getHeight();
    Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
    Bitmap overlayScaledBitmap = Bitmap.createScaledBitmap(overlayBitmap, wid, hgt, true);
    Canvas canvas = new Canvas(newImage);
    canvas.drawBitmap(cameraScaledBitmap , 0, 0, null);
    canvas.drawBitmap(overlayScaledBitmap , 0, 0, null);

    File storagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    storagePath.mkdirs();
    String finalName = Long.toString(System.currentTimeMillis());
    File myImage = new File(storagePath, finalName + ".jpg");

    String photoPath = Environment.getExternalStorageDirectory().getAbsolutePath() +"/" + finalName + ".jpg";

    try {
        FileOutputStream fos = new FileOutputStream(myImage);
        newImage.compress(Bitmap.CompressFormat.JPEG, 80, fos);
        fos.close();
    } catch (IOException e) {
        Toast.makeText(this, "Pic not saved", Toast.LENGTH_SHORT).show();
        return;
    }
    Toast.makeText(this, "Pic saved in: " + photoPath, Toast.LENGTH_SHORT).show();

    camera.startPreview();
    newImage.recycle();
    newImage = null;
    cameraBitmap.recycle();
    cameraBitmap = null;
}

这篇关于保存相机preVIEW与叠加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆