1

カメラを開いて背景として使用される写真を撮るためのボタンと、上に座って移動できるアイテムを追加するための別のボタンを使用して、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();
    }
}

これが他の画像を上に描画しない理由がわかりません...

4

1 に答える 1

1

私の場合、これを使用しています:

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;
}
于 2012-05-24T10:33:20.933 に答える