gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:11:51 25 4
gpt4 key购买 nike

我启动并运行了我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com