下面列出了java.nio.IntBuffer#wrap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Buffer getPixels(final Bitmap pBitmap, final PixelFormat pPixelFormat, final ByteOrder pByteOrder) {
final int[] pixelsARGB_8888 = GLHelper.getPixelsARGB_8888(pBitmap);
switch(pPixelFormat) {
case RGB_565:
return ShortBuffer.wrap(GLHelper.convertARGB_8888toRGB_565(pixelsARGB_8888, pByteOrder)); // TODO Is ShortBuffer or IntBuffer faster?
case RGBA_8888:
// HACK =(
final ByteOrder reverseByteOrder = (pByteOrder == ByteOrder.LITTLE_ENDIAN) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
return IntBuffer.wrap(GLHelper.convertARGB_8888toRGBA_8888(pixelsARGB_8888, reverseByteOrder));
case RGBA_4444:
return ShortBuffer.wrap(GLHelper.convertARGB_8888toRGBA_4444(pixelsARGB_8888, pByteOrder)); // TODO Is ShortBuffer or IntBuffer faster?
case A_8:
return ByteBuffer.wrap(GLHelper.convertARGB_8888toA_8(pixelsARGB_8888));
default:
throw new IllegalArgumentException("Unexpected " + PixelFormat.class.getSimpleName() + ": '" + pPixelFormat + "'.");
}
}
private Bitmap createBitmapFromGLSurface(int w, int h, GL10 gl) {
int bitmapBuffer[] = new int[w * h];
int bitmapSource[] = new int[w * h];
IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
intBuffer.position(0);
try {
gl.glReadPixels(0, 0, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
int offset1, offset2, texturePixel, blue, red, pixel;
for (int i = 0; i < h; i++) {
offset1 = i * w;
offset2 = (h - i - 1) * w;
for (int j = 0; j < w; j++) {
texturePixel = bitmapBuffer[offset1 + j];
blue = (texturePixel >> 16) & 0xff;
red = (texturePixel << 16) & 0x00ff0000;
pixel = (texturePixel & 0xff00ff00) | red | blue;
bitmapSource[offset2 + j] = pixel;
}
}
} catch (GLException e) {
Log.e("CreateBitmap", "createBitmapFromGLSurface: " + e.getMessage(), e);
return null;
}
return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
}
/**
* Return an IntBuffer that accesses the major version number.
* This is used to create a Monitor object for this value.
*
* @return IntBuffer - a ByteBuffer that accesses the major version number
* in the instrumentation buffer header.
*/
public IntBuffer majorVersionBuffer() {
int[] holder = new int[1];
holder[0] = getMajorVersion();
IntBuffer ib = IntBuffer.wrap(holder);
ib.limit(1);
return ib;
}
/**
* Return an IntBuffer that accesses the major version number.
* This is used to create a Monitor object for this value.
*
* @return IntBuffer - a ByteBuffer that accesses the major version number
* in the instrumentation buffer header.
*/
public IntBuffer majorVersionBuffer() {
int[] holder = new int[1];
holder[0] = getMajorVersion();
IntBuffer ib = IntBuffer.wrap(holder);
ib.limit(1);
return ib;
}
public static Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, int glHeight)
throws OutOfMemoryError {
int bitmapBuffer[] = new int[w * h];
int bitmapSource[] = new int[w * h];
IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
intBuffer.position(0);
try {
GLES11.glReadPixels(x, glHeight - h - y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
int offset1, offset2;
for (int i = 0; i < h; i++) {
offset1 = i * w;
offset2 = (h - i - 1) * w;
for (int j = 0; j < w; j++) {
int texturePixel = bitmapBuffer[offset1 + j];
int blue = (texturePixel >> 16) & 0xff;
int red = (texturePixel << 16) & 0x00ff0000;
int pixel = (texturePixel & 0xff00ff00) | red | blue;
bitmapSource[offset2 + j] = pixel;
}
}
} catch (GLException e) {
return null;
}
return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
}
/**
* Return an IntBuffer that accesses the major version number.
* This is used to create a Monitor object for this value.
*
* @return IntBuffer - a ByteBuffer that accesses the major version number
* in the instrumentation buffer header.
*/
public IntBuffer majorVersionBuffer() {
int[] holder = new int[1];
holder[0] = getMajorVersion();
IntBuffer ib = IntBuffer.wrap(holder);
ib.limit(1);
return ib;
}
/**
* Return an IntBuffer that accesses the minor version number.
* This is used to create a Monitor object for this value.
*
* @return IntBuffer - a ByteBuffer that accesses the minor version number
* in the instrumentation buffer header.
*/
public IntBuffer minorVersionBuffer() {
int[] holder = new int[1];
holder[0] = getMinorVersion();
IntBuffer ib = IntBuffer.wrap(holder);
ib.limit(1);
return ib;
}
public static void decodeBlockPack(
IntBuffer src,
IntFilterFactory filterFactory,
IntBitPacking packer,
IntOutputStream dst)
{
// Fetch length of original array.
if (!src.hasRemaining()) {
return;
}
final int outLen = (int)src.get() - 1;
// Fetch and output first int, and set it as delta's initial context.
final int first = src.get();
dst.write(first);
IntFilter filter = filterFactory.newFilter(first);
// Decompress intermediate blocks.
final int chunkSize = packer.getBlockSize();
final int chunkNum = outLen / chunkSize;
if (chunkNum > 0) {
packer.decompress(src, dst, filter, chunkNum);
}
// Decompress last block.
final int chunkRemain = outLen % chunkSize;
if (chunkRemain > 0) {
int[] last = new int[chunkSize];
IntBuffer buf = IntBuffer.wrap(last);
packer.decompress(src, new IntBufferOutputStream(buf),
filter, 1);
dst.write(last, 0, chunkRemain);
}
}
public int[] getPixelsARGB_8888(final GLState pGLState, final int pX, final int pY, final int pWidth, final int pHeight) {
final int[] pixelsRGBA_8888 = new int[pWidth * pHeight];
final IntBuffer glPixelBuffer = IntBuffer.wrap(pixelsRGBA_8888);
glPixelBuffer.position(0);
this.begin(pGLState);
GLES20.glReadPixels(pX, pY, pWidth, pHeight, this.mPixelFormat.getGLFormat(), this.mPixelFormat.getGLType(), glPixelBuffer);
this.end(pGLState);
return GLHelper.convertRGBA_8888toARGB_8888(pixelsRGBA_8888);
}
/**
* Return an IntBuffer that accesses the major version number.
* This is used to create a Monitor object for this value.
*
* @return IntBuffer - a ByteBuffer that accesses the major version number
* in the instrumentation buffer header.
*/
public IntBuffer majorVersionBuffer() {
int[] holder = new int[1];
holder[0] = getMajorVersion();
IntBuffer ib = IntBuffer.wrap(holder);
ib.limit(1);
return ib;
}
/**
* Return an IntBuffer that accesses the minor version number.
* This is used to create a Monitor object for this value.
*
* @return IntBuffer - a ByteBuffer that accesses the minor version number
* in the instrumentation buffer header.
*/
public IntBuffer minorVersionBuffer() {
int[] holder = new int[1];
holder[0] = getMinorVersion();
IntBuffer ib = IntBuffer.wrap(holder);
ib.limit(1);
return ib;
}
/**
* Return an IntBuffer that accesses the minor version number.
* This is used to create a Monitor object for this value.
*
* @return IntBuffer - a ByteBuffer that accesses the minor version number
* in the instrumentation buffer header.
*/
public IntBuffer minorVersionBuffer() {
int[] holder = new int[1];
holder[0] = getMinorVersion();
IntBuffer ib = IntBuffer.wrap(holder);
ib.limit(1);
return ib;
}
@Override
public void accept(ImageBuffer buffer) {
final int imagewidth = buffer.bitmap.getWidth(),
imageheight = buffer.bitmap.getHeight(),
imagesize = imagewidth * imageheight + imagewidth * 4;
// Change the buffer dimensions
if (buffer.image == null || buffer.image.array().length < imagesize) {
buffer.image = IntBuffer.wrap(new int[imagesize]);
}
buffer.imagewidth = imagewidth;
buffer.imageheight = imageheight;
buffer.bitmap.copyPixelsToBuffer(buffer.image);
}
/**
* Return an IntBuffer that accesses the major version number. This is used to create a Monitor object for this value.
*
* @return IntBuffer - a ByteBuffer that accesses the major version number in the instrumentation buffer header.
*/
public IntBuffer majorVersionBuffer() {
int[] holder = new int[1];
holder[0] = getMajorVersion();
IntBuffer ib = IntBuffer.wrap(holder);
ib.limit(1);
return ib;
}
/**
* Return an IntBuffer that accesses the minor version number.
* This is used to create a Monitor object for this value.
*
* @return IntBuffer - a ByteBuffer that accesses the minor version number
* in the instrumentation buffer header.
*/
public IntBuffer minorVersionBuffer() {
int[] holder = new int[1];
holder[0] = getMinorVersion();
IntBuffer ib = IntBuffer.wrap(holder);
ib.limit(1);
return ib;
}
@Test
public void methodOfNioBufferWithCovariantTypes_afterDesugarInvocationOfIntBufferMethod(
@RuntimeMethodHandle(className = "NioBufferInvocations", memberName = "getIntBufferPosition")
MethodHandle after)
throws Throwable {
IntBuffer buffer = IntBuffer.wrap(new int[] {10, 20, 30});
int expectedPos = 2;
IntBuffer result = (IntBuffer) after.invoke(buffer, expectedPos);
assertThat(result.position()).isEqualTo(expectedPos);
}
/**
* Return an IntBuffer that accesses the major version number.
* This is used to create a Monitor object for this value.
*
* @return IntBuffer - a ByteBuffer that accesses the major version number
* in the instrumentation buffer header.
*/
public IntBuffer majorVersionBuffer() {
int[] holder = new int[1];
holder[0] = getMajorVersion();
IntBuffer ib = IntBuffer.wrap(holder);
ib.limit(1);
return ib;
}
/**
* Return an IntBuffer that accesses the minor version number.
* This is used to create a Monitor object for this value.
*
* @return IntBuffer - a ByteBuffer that accesses the minor version number
* in the instrumentation buffer header.
*/
public IntBuffer minorVersionBuffer() {
int[] holder = new int[1];
holder[0] = getMinorVersion();
IntBuffer ib = IntBuffer.wrap(holder);
ib.limit(1);
return ib;
}
CursorImageData(BufferedImage[] bi, int delay, int hsX, int hsY, int curType) {
// cursor type
// 0 - Undefined (an array of images inside an ICO)
// 1 - ICO
// 2 - CUR
IntBuffer singleCursor = null;
ArrayList<IntBuffer> cursors = new ArrayList<IntBuffer>();
int bwidth = 0;
int bheight = 0;
boolean multIcons = false;
// make the cursor image
for (int i = 0; i < bi.length; i++) {
BufferedImage img = bi[i];
bwidth = img.getWidth();
bheight = img.getHeight();
if (curType == 1) {
hsX = 0;
hsY = bheight - 1;
} else if (curType == 2) {
if (hsY == 0) {
// make sure we flip if 0
hsY = bheight - 1;
}
} else {
// We force to choose 32x32 icon from
// the array of icons in that ICO file.
if (bwidth != 32 && bheight != 32) {
multIcons = true;
continue;
} else {
if (img.getType() != 2) {
continue;
} else {
// force hotspot
hsY = bheight - 1;
}
}
}
// We flip our image because .ICO and .CUR will always be reversed.
AffineTransform trans = AffineTransform.getScaleInstance(1, -1);
trans.translate(0, -img.getHeight(null));
AffineTransformOp op = new AffineTransformOp(trans, AffineTransformOp.TYPE_BILINEAR);
img = op.filter(img, null);
singleCursor = BufferUtils.createIntBuffer(img.getWidth() * img.getHeight());
DataBufferInt dataIntBuf = (DataBufferInt) img.getData().getDataBuffer();
singleCursor = IntBuffer.wrap(dataIntBuf.getData());
cursors.add(singleCursor);
}
int count;
if (multIcons) {
bwidth = 32;
bheight = 32;
count = 1;
} else {
count = cursors.size();
}
// put the image in the IntBuffer
data = BufferUtils.createIntBuffer(bwidth * bheight);
imgDelay = BufferUtils.createIntBuffer(bi.length);
for (int i = 0; i < count; i++) {
data.put(cursors.get(i));
if (delay > 0) {
imgDelay.put(delay);
}
}
width = bwidth;
height = bheight;
xHotSpot = hsX;
yHotSpot = hsY;
numImages = count;
data.rewind();
if (imgDelay != null) {
imgDelay.rewind();
}
}
@Override
public void drawFrame() {
if(frameBuffer == null) {
if(getWidth() != 0 && getHeight() != 0) {
initFBO();
} else {
return;
}
}
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffer[0]);
super.drawFrame();
int[] pixels = new int[getWidth()*getHeight()];
IntBuffer intBuffer = IntBuffer.wrap(pixels);
intBuffer.position(0);
GLES20.glReadPixels(0, 0, getWidth(), getHeight(), GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, intBuffer);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
for(int i = 0; i < pixels.length; i++) {
pixels[i] = (pixels[i] & (0xFF00FF00)) | ((pixels[i] >> 16) & 0x000000FF) | ((pixels[i] << 16) & 0x00FF0000); //swap red and blue to translate back to bitmap rgb style
}
Bitmap image = Bitmap.createBitmap(pixels, getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
callback.bitmapCreated(image);
}