gldsurfaceview의 출력을 sdCard의 PNG로 렌더링하려고하는데 문제가 있습니다. 비슷한 며칠을 정렬하려고 며칠을 보냈는데 이것은 내 수준의 전문성을 뛰어 넘었습니다. 누군가가 나를 아래의 로그를 통해 정렬 도와 줄 수 있고 내가 잘못 될 수도 있습니다 볼 수 있습니다.GLSURFACEVIEW에서 PNG로 저장 - 오류 glReadPixels
감사합니다.
로그 :
12-16 12:09:18.831: E/AndroidRuntime(29864): FATAL EXCEPTION: GLThread 2712
12-16 12:09:18.831: E/AndroidRuntime(29864): java.nio.BufferUnderflowException
12-16 12:09:18.831: E/AndroidRuntime(29864): at java.nio.Buffer.checkGetBounds(Buffer.java:177)
12-16 12:09:18.831: E/AndroidRuntime(29864): at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:66)
12-16 12:09:18.831: E/AndroidRuntime(29864): at java.nio.IntToByteBufferAdapter.get(IntToByteBufferAdapter.java:105)
12-16 12:09:18.831: E/AndroidRuntime(29864): at java.nio.IntBuffer.get(IntBuffer.java:234)
12-16 12:09:18.831: E/AndroidRuntime(29864): at com.research.glgrade.GLLayer.grabPixels(GLLayer.java:865)
12-16 12:09:18.831: E/AndroidRuntime(29864): at com.research.glgrade.GLLayer.saveScreenShot(GLLayer.java:810)
12-16 12:09:18.831: E/AndroidRuntime(29864): at com.research.glgrade.GLLayer.onDrawFrame(GLLayer.java:794)
12-16 12:09:18.831: E/AndroidRuntime(29864): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1527)
12-16 12:09:18.831: E/AndroidRuntime(29864): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
12-16 12:09:26.849: I/Choreographer(29864): Skipped 478 frames! The application may be doing too much work on its main thread.
여기에 내 현재 코드 : 주요 활동에서 :
GlobalVariables.setPrint("true");
mView.requestRender();
가에서 렌더링 클래스
public void onDrawFrame(GL10 glUnused) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
final String vertexShader = getVertexShader();
final String fragmentShader = getFragmentShader();
final int vertexShaderHandle = ShaderHelper.compileShader(
GLES20.GL_VERTEX_SHADER, vertexShader);
final int fragmentShaderHandle = ShaderHelper.compileShader(
GLES20.GL_FRAGMENT_SHADER, fragmentShader);
mProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle,
fragmentShaderHandle, new String[] { "a_Position",
"a_TexCoordinate" });
// Set our per-vertex lighting program.
GLES20.glUseProgram(mProgramHandle);
// Set program handles for cube drawing.
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle,
"u_MVPMatrix");
mTextureUniformHandle0 = GLES20.glGetUniformLocation(mProgramHandle,
"u_Texture0");
mTextureUniformHandle1 = GLES20.glGetUniformLocation(mProgramHandle,
"u_Texture1");
GLES20.glActiveTexture(GLES20.GL_TEXTURE4);
// Bind the texture to this unit.
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle4);
// Tell the texture uniform sampler to use this texture in the shader by
// binding to texture unit 3.
GLES20.glUniform1i(mTextureUniformHandle4, 4);
// Draw some cubes.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, mTransX, mTransY, mAngle*0.05f);
Matrix.rotateM(mModelMatrix, 0, 0.0f, 1.0f, 1.0f, 0.0f);
drawCube();
int width_surfacea = width_surface ;
int height_surfacea = height_surface ;
if (GlobalVariables.getPrint() != "false") {
String mFrameCount = "1";
saveScreenShot(0, 0, width_surfacea, height_surfacea, "/save/test.png");
GlobalVariables.setPrint("false");
}
}
public void saveScreenShot(int x, int y, int w, int h, String filename) {
Bitmap bmp = grabPixels(x, y, w, h);
try {
String path = Environment.getExternalStorageDirectory() + "/" + filename;
File file = new File(path);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Bitmap grabPixels(int x, int y, int w, int h) {
int screenshotSize = w * h;
ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 3);
bb.order(ByteOrder.nativeOrder());
bb.position(0);
GLES20.glReadPixels(0, 0, w, h, GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, bb);
int pixelsBuffer[] = new int[screenshotSize];
bb.asIntBuffer().get(pixelsBuffer);
bb = null;
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
bitmap.setPixels(pixelsBuffer, screenshotSize-w, -w, 0, 0, w, h);
pixelsBuffer = null;
short sBuffer[] = new short[screenshotSize];
ShortBuffer sb = ShortBuffer.wrap(sBuffer);
bitmap.copyPixelsToBuffer(sb);
//Making created bitmap (from OpenGL points) compatible with Android bitmap
for (int i = 0; i < screenshotSize; ++i) {
short v = sBuffer[i];
sBuffer[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
}
sb.rewind();
bitmap.copyPixelsFromBuffer(sb);
return bitmap;
}
GL 컨텍스트를 선택한 경우 어떤 픽셀 형식을 선택하셨습니까? –
내 주요 활동 onCreate에서 mView를 mView.setZOrderOnTop (false)로 정의합니다. mView.setEGLContextClientVersion (2); mView.setRenderer (새 GLLayer (this)); mView.setDebugFlags (GLSurfaceView.DEBUG_CHECK_GL_ERROR); //mView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); mView.setRenderMode (GLSurfaceView.RENDERMODE_CONTINUOUSLY); – user1540142
GLSurfaceview에 대한 특정 Color 정의가 없습니다. – user1540142