Android:从画布上的相机保存图像

时间:2012-08-07 16:38:18

标签: android image android-camera android-canvas

我需要从相机中获取图像并将其保存在另一张图像中。我怎样才能做到这一点?然后我想将这个新图像保存在SD卡上。

1 个答案:

答案 0 :(得分:1)

1)这是一个如何使用android的相机的教程: tutorial

2)要将相机图像叠加在位图上,您必须: - 创建位图 - 创建一个带有该位图引用的画布 - 将相机中的图片绘制到画布上。 (由于在使用画布时没有复制此位图,因此更改将应用​​于您创建的位图。)

3)要保存位图,可以使用我写的这个方法:

/**
     * <b><i>public void writeBitmapToMemory(String filename, Bitmap bitmap)</i></b>
     * <br>
     * Since: API 1
     * <br>
     * <br>
     * Write a bitmap to the phone's internal storage.
     * 
     * @param filename 
     * The name of the file you wish to write to.
     *
     *      
     */

    public void writeBitmapToMemory(String filename, Bitmap bitmap) {
        FileOutputStream fos;
        // Use the compress method on the Bitmap object to write image to the OutputStream
        try {
            fos = game.openFileOutput(filename, Context.MODE_PRIVATE);
            // Writing the bitmap to the output stream
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
            // this.gameEngineLog.d(classTAG, "Bitmap successfully written: " + filename);
        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();
            // this.gameEngineLog.d(classTAG, "Bitmap couldn't be written: " + filename);

        } 
        catch (IOException e) {
            e.printStackTrace();
            this.gameEngineLog.d(classTAG, "Bitmap couldn't be written: " + filename);

        }

    }