android - 将 Android SurfaceView 保存到 Bitmap 并且缺少一些元素

标签 android canvas bitmap surfaceview

我启动并运行了我的 SurfaceView,其中有一个按钮可以打开相机并拍摄用作背景的照片,另一个按钮可以添加位于顶部并可以四处移动的项目。这一切都很好,直到我尝试将 SurfaceView 保存为位图时,我得到的只是背景,上面没有图像。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(_mGotImage){

        canvas.drawBitmap(_mImage, 0, 0, null);
    }else{

            canvas.drawColor(Color.BLACK);
    }

    //if the array is not empty
    if(!_mJazzItems.isEmpty()){

        //step through each item in the array
        for(JazzItem item: _mJazzItems){

            //get the bitmap it is using
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), item.getBitmap());
            //and draw that bitmap at its X and Y coords
            canvas.drawBitmap(bitmap, item.getX(), item.getY(), null);

        }
    }
}

这是尝试保存 Canvas 时调用的方法。

    public void screenGrab(){

    Bitmap image = Bitmap.createBitmap(_mPanelWidth, _mPanelHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(image);
    this.onDraw(canvas);

    String path=Environment.getExternalStorageDirectory() + "/test2.png";
    File file = new File(path);

    try{

        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        image.compress(CompressFormat.PNG, 100, ostream);
        ostream.flush();
        ostream.close();

    }catch (Exception e){

        e.printStackTrace();
    }
}

onDraw 工作正常,我在后台拍摄我的相机,可以将我的所有项目添加到顶部并四处移动。就在我尝试获取屏幕截图时,上面的所有项目都不存在。

感谢您的帮助!

-- 更新--

我已经将屏幕抓取方法修改为:

    public void screenGrab(){

    Bitmap image = Bitmap.createBitmap(_mPanelWidth, _mPanelHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(image);

    canvas.drawBitmap(_mImage, 0, 0, null);

    //if the array is not empty
    if(!_mJazzItems.isEmpty()){

        //step through each item in the array
        for(JazzItem item: _mJazzItems){

            //get the bitmap it is using
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), item.getBitmap());
            //and draw that bitmap at its X and Y coords
            canvas.drawBitmap(bitmap, item.getX(), item.getY(), null);

        }
    }

    String path=Environment.getExternalStorageDirectory() + "/test2.png";
    File file = new File(path);

    try{

        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        image.compress(CompressFormat.PNG, 100, ostream);
        ostream.flush();
        ostream.close();

    }catch (Exception e){

        e.printStackTrace();
    }
}

我不明白为什么这不是在顶部绘制其他图像...

最佳答案

在我的例子中,我使用的是:

public static Bitmap combineImages(Bitmap c, Bitmap overLayImage, Context con) {
    Bitmap cs = null;
    int width, height = 0;
    width     = c.getWidth();
    height     = c.getHeight();
    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas comboImage = new Canvas(cs);
    comboImage.drawBitmap(c, 0, 0, null);
    String left = yourleftPosition;
    String top = yourtopPosition;

    comboImage.drawBitmap(overLayImage, Float.parseFloat(left), Float.parseFloat(top),null);
    /******
    *
    * Write file to SDCard
    *
    * ****/
    String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
    OutputStream os = null;
    try {
        String pathis = Environment.getExternalStorageDirectory()
        + "/DCIM/Camera/" + tmpImg;
        os = new FileOutputStream(pathis);
        cs.compress(CompressFormat.PNG, 100, os);
    }
    catch (IOException e) {
        Log.e("combineImages", "problem combining images", e);
    }
    return cs;
}

关于android - 将 Android SurfaceView 保存到 Bitmap 并且缺少一些元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10735448/

相关文章:

javascript - 如何在 Java 中使用 Selenium 获取 Canvas 元素内图像的 URL,以及如何使用 JavascriptExecutor?

delphi - 将位图转换为 RTF 后,图像在 ms word 中不显示(但在写字板中显示)

java - Kotlin硬编码字符串

javascript - Fabric js loadFromJSON 问题

javascript - 为什么 canvas.toDataURL() 抛出安全异常?

java - 如何以编程方式旋转存储为一维数组的位图图像?

java - 如何获得准确的位图文件大小?

android - Android 相机 API 的包装器库

android - 我们如何识别密码类型

android - Web Speech api 不适用于 android chrome