使用叠加图像保存cameraPreview

时间:2014-03-18 10:32:47

标签: android camera overlay preview

我正在使用Google的CameraPreview示例演示。在这里,我有cameraPreview和cameraPreview上的叠加图像。

当我拍摄照片时,只保存了cameraPreview,但我需要将叠加图像保存在cameraPreview上。

这是我保存cameraPreview的照片的方式,但我需要知道什么是覆盖展位图像并将其保存为一个的最佳方式。

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

1 个答案:

答案 0 :(得分:2)

我终于这样做了:

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