科科斯2D屏幕截图是黑色的Android [英] COCOS 2D screen shot is black in Android
本文介绍了科科斯2D屏幕截图是黑色的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的code。图像保存,但它是黑色的。照片
请参阅我的code,告诉我哪里做错了。
我用这code在菜单中。
I am using the following code. The image is saved but it is BLACK.
Please see my code and tell me where I am doing wrong.
I am using this code in the Menu.
case R.id.id_menu_Save:
Bitmap bmp = SavePixels(0, 0, 800, 400, CCDirector.sharedDirector().gl);
File file = new File("/sdcard/test.jpg");
try
{
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(CompressFormat.JPEG, 100, fos);
Toast.makeText(getApplicationContext(), "Image Saved", 0).show();
Log.i("Menu Save Button", "Image saved as JPEG");
}
catch (Exception e)
{
e.printStackTrace();
}
break;
这是我保存图像的功能。
This is my Save Image Function.
public static Bitmap SavePixels(int x, int y, int w, int h, GL10 gl)
{
int b[]=new int[w*(y+h)];
int bt[]=new int[w*h];
IntBuffer ib=IntBuffer.wrap(b);
ib.position(0);
gl.glReadPixels(x, 0, w, y+h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
for(int i=0, k=0; i<h; i++, k++)
{//remember, that OpenGL bitmap is incompatible with Android bitmap
//and so, some correction need.
for(int j=0; j<w; j++)
{
int pix=b[i*w+j];
int pb=(pix>>16)&0xff;
int pr=(pix<<16)&0x00ff0000;
int pix1=(pix&0xff00ff00) | pr | pb;
bt[(h-k-1)*w+j]=pix1;
}
}
Bitmap sb = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
return sb;
}
除了上面,我想从你是指我在正确的方向。就像如果我要得到屏幕的像素,哪个类/实体我应该探索?
Apart from above, what I want from you is to point me in the right direction. Like if I have to get the pixels of the screen, what class/entity should I be exploring?
推荐答案
只要改变SavePixelx方法将如下:
Just change SavePixelx method to the below:
public static Bitmap SavePixels(int x, int y, int w, int h, GL10 gl)
{
int b[]=new int[w*(y+h)];
int bt[]=new int[w*h];
IntBuffer ib=IntBuffer.wrap(b);
ib.position(0);
gl.glReadPixels(x, 0, w, y+h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
for(int i=0, k=0; i<h; i++, k++)
{
//remember, that OpenGL bitmap is incompatible with Android bitmap
//and so, some correction need.
for(int j=0; j<w; j++)
{
int pix=b[i*w+j];
int pb=(pix>>16)&0xff;
int pr=(pix<<16)&0xffff0000;
int pix1=(pix&0xff00ff00) | pr | pb;
bt[(h-k-1)*w+j]=pix1;
}
}
Bitmap sb = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
return sb;
}
这篇关于科科斯2D屏幕截图是黑色的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文