下面列出了java.nio.IntBuffer#array ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Bitmap captureBitmap() throws OutOfMemoryError {
int width = (int) mViewportRect.width();
int height = (int) mViewportRect.height();
final IntBuffer pixelBuffer = IntBuffer.allocate(width * height);
GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixelBuffer);
int[] pixelArray = pixelBuffer.array();
final int[] pixelMirroredArray = new int[width * height];
// Convert upside down mirror-reversed image to right-side up normal image.
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixelMirroredArray[(height - i - 1) * width + j] = pixelArray[i * width + j];
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(IntBuffer.wrap(pixelMirroredArray));
return bitmap;
}
private void convertToBitmap() {
int[] iat = new int[mWidth * mHeight];
IntBuffer ib = IntBuffer.allocate(mWidth * mHeight);
mGL.glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, ib);
int[] ia = ib.array();
//Stupid !
// Convert upside down mirror-reversed image to right-side up normal
// image.
for (int i = 0; i < mHeight; i++) {
for (int j = 0; j < mWidth; j++) {
iat[(mHeight - i - 1) * mWidth + j] = ia[i * mWidth + j];
}
}
mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
mBitmap.copyPixelsFromBuffer(IntBuffer.wrap(iat));
}
public static void captureImage(int width, int height) throws InterruptedException {
final Semaphore waiter = new Semaphore(0);
// Take picture on OpenGL thread
final int[] pixelMirroredArray = new int[width * height];
final IntBuffer pixelBuffer = IntBuffer.allocate(width * height);
GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixelBuffer);
int[] pixelArray = pixelBuffer.array();
// Convert upside down mirror-reversed image to right-side up normal image.
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixelMirroredArray[(height - i - 1) * width + j] = pixelArray[i * width + j];
}
}
waiter.release();
waiter.acquire();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(IntBuffer.wrap(pixelMirroredArray));
saveBitmap(bitmap, new File(Environment.getExternalStorageDirectory(), "videoeditor/tmp.png"));
}
private void convertToBitmap() {
int[] iat = new int[mWidth * mHeight];
IntBuffer ib = IntBuffer.allocate(mWidth * mHeight);
mGL.glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, ib);
int[] ia = ib.array();
//Stupid !
// Convert upside down mirror-reversed image to right-side up normal
// image.
for (int i = 0; i < mHeight; i++) {
for (int j = 0; j < mWidth; j++) {
iat[(mHeight - i - 1) * mWidth + j] = ia[i * mWidth + j];
}
}
mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
mBitmap.copyPixelsFromBuffer(IntBuffer.wrap(iat));
}
private void convertToBitmap() {
int[] iat = new int[mWidth * mHeight];
IntBuffer ib = IntBuffer.allocate(mWidth * mHeight);
mGL.glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, ib);
int[] ia = ib.array();
//Stupid !
// Convert upside down mirror-reversed image to right-side up normal
// image.
for (int i = 0; i < mHeight; i++) {
for (int j = 0; j < mWidth; j++) {
iat[(mHeight - i - 1) * mWidth + j] = ia[i * mWidth + j];
}
}
mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
mBitmap.copyPixelsFromBuffer(IntBuffer.wrap(iat));
}
public Bitmap capture(int width, int height) {
mPipeline.onSurfaceChanged(null, width, height);
mPipeline.startRender();
mPipeline.onDrawFrame(null);
int[] iat = new int[mWidth * mHeight];
IntBuffer ib = IntBuffer.allocate(width * height);
GLES20.glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, ib);
int[] ia = ib.array();
for (int i = 0; i < mHeight; i++) {
System.arraycopy(ia, i * mWidth, iat, (mHeight - i - 1) * mWidth, mWidth);
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(IntBuffer.wrap(iat));
mPipeline.onSurfaceDestroyed();
// 释放EGL环境
mInputSurface.release();
mEgl.release();
return bitmap;
}
/**
* ByteChannelからint配列を読み込む
* @param channel
* @return
* @throws IOException
*/
public static int[] readIntArray(@NonNull final ByteChannel channel)
throws IOException {
final int n = readInt(channel);
final ByteBuffer buf = ByteBuffer.allocate(n * 4).order(ByteOrder.BIG_ENDIAN);
final int readBytes = channel.read(buf);
if (readBytes != n * 4) throw new IOException();
buf.clear();
final IntBuffer result = buf.asIntBuffer();
if (result.hasArray()) {
return result.array();
} else {
final int[] b = new int[n];
result.get(b);
return b;
}
}
public Identicon(String fullName, char[] masterPassword) {
this.fullName = fullName;
byte[] masterPasswordBytes = charset.encode(CharBuffer.wrap(masterPassword)).array();
ByteBuffer identiconSeedBytes = ByteBuffer.wrap(
MessageAuthenticationDigests.HmacSHA256.of(masterPasswordBytes, fullName.getBytes(charset)));
Arrays.fill(masterPasswordBytes, (byte) 0);
IntBuffer identiconSeedBuffer = IntBuffer.allocate(identiconSeedBytes.capacity());
while (identiconSeedBytes.hasRemaining())
identiconSeedBuffer.put(identiconSeedBytes.get() & 0xFF);
int[] identiconSeed = identiconSeedBuffer.array();
color = colors[identiconSeed[4] % colors.length];
text = strf("%c%c%c%c", leftArm[identiconSeed[0] % leftArm.length], body[identiconSeed[1] % body.length],
rightArm[identiconSeed[2] % rightArm.length], accessory[identiconSeed[3] % accessory.length]);
}
private void convertToBitmap(Bitmap bitmap) {
final int w = bitmap.getWidth();
final int h = bitmap.getHeight();
IntBuffer ib = IntBuffer.allocate(w * h);
GLES20.glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, ib);
int[] ia = ib.array();
bitmap.copyPixelsFromBuffer(IntBuffer.wrap(ia));
}
public static Bitmap getScreenShot(int width, int height){
IntBuffer pixelBuffer = IntBuffer.allocate(width * height);
GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE,
pixelBuffer);
int[] pixelMirroredArray = new int[width * height];
int[] pixelArray = pixelBuffer.array();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixelMirroredArray[(height - i - 1) * width + j] = pixelArray[i * width + j];
}
}
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
return bmp;
}
/**
* Construct a new block change array from the copy of a given data array.
* @param data - the data array to store internally.
*/
public BlockChangeArray(byte[] input) {
if ((input.length % RECORD_SIZE) != 0)
throw new IllegalArgumentException("The lenght of the input data array should be a multiple of " + RECORD_SIZE + ".");
IntBuffer source = ByteBuffer.wrap(input).asIntBuffer();
IntBuffer destination = IntBuffer.allocate(input.length / RECORD_SIZE);
destination.put(source);
// Get the copied array
data = destination.array();
}
@Override
public void bufferOutput(IntBuffer buffer) {
int width = getWidth();
int height = getHeight();
if (width <= 0 || height <= 0) {
if (mCallback != null) {
mCallback.bitmapOutput(null);
}
return;
}
try {
int[] pixels = buffer.array();
// 方案一,使用copyPixelsFromBuffer,是方案二速度的2倍以上
Bitmap bitmap = Bitmap.createBitmap(width, height, mConfig);
bitmap.copyPixelsFromBuffer(IntBuffer.wrap(pixels));
// 方案二,手动转换像素数组
// for (int i = 0; i < pixels.length; i++) {
// // glReadPixels设置GLES20.GL_RGBA时,读取出来格式为ABGR,要转换为Bitmap需要的ARGB,同时设置Alpha值为1
// pixels[i] = (0xFF000000)
// | ((pixels[i] << 16) & 0x00FF0000)
// | (pixels[i] & (0xFF00FF00))
// | ((pixels[i] >> 16) & 0x000000FF);
// }
// Bitmap bitmap = Bitmap.createBitmap(pixels, width, height, mConfig);
if (mCallback != null) {
mCallback.bitmapOutput(bitmap);
}
} catch (OutOfMemoryError e) {
e.printStackTrace();
if (mCallback != null) {
mCallback.bitmapOutput(null);
}
}
}
static
public int[] toIntArray(Tensor tensor){
IntBuffer intBuffer = IntBuffer.allocate(tensor.numElements());
tensor.writeTo(intBuffer);
return intBuffer.array();
}
/**
* For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers
* backed with an array, allocates enough memory for the array and copies it.
*
* @param buffer the Buffer to reference or copy
* @see #put(int[])
*/
public IntPointer(IntBuffer buffer) {
super(buffer);
if (buffer != null && buffer.hasArray()) {
int[] array = buffer.array();
allocateArray(array.length);
put(array);
position(buffer.position());
limit(buffer.limit());
}
}
public static int[] bin2i32a(byte[] bin) {
int l = (int) (4 * Math.ceil((double) bin.length / 4));
byte[] new_bin = Arrays.copyOfRange(bin, 0, l);
bin = new_bin;
ByteBuffer bin_buffer = ByteBuffer.wrap(bin);
IntBuffer int_buffer = bin_buffer.asIntBuffer();
if (int_buffer.hasArray()) {
return int_buffer.array();
} else {
ArrayList<Integer> list = new ArrayList<>();
while (int_buffer.hasRemaining()) {
list.add(int_buffer.get());
}
int[] aux = new int[list.size()];
for (int i = 0; i < aux.length; i++) {
aux[i] = list.get(i);
}
return aux;
}
}
@Override
public List<Field> createListField(Tensor<Integer> tensor, IntBuffer intBuffer) {
List<Field> fields = new ArrayList<>();
tensor.writeTo(intBuffer);
int[] ints = intBuffer.array();
for (int aInt : ints) {
fields.add(Field.create(aInt));
}
return fields;
}
public static long compute(IntBuffer buff) {
buff.rewind();
ByteBuffer bBuff = ByteBuffer.allocate(buff.array().length * 4);
for (int i : buff.array()) {
bBuff.putInt(i);
}
return compute(bBuff);
}
/**
* Create a new {@link JTensor} instance by extracting data from the underlying {@link Tensor} and
* closing it afterwards.
*/
public static JTensor create(final Tensor<?> tensor) {
final JTensor jt;
try {
switch (tensor.dataType()) {
case STRING:
if (tensor.numDimensions() == 0) {
final String value = new String(tensor.bytesValue(), UTF_8);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), value);
} else {
final int[] dimensions = toIntExact(tensor.shape());
final Object byteArray =
tensor.copyTo(Array.newInstance(byte[].class, toIntExact(tensor.shape())));
jt =
new AutoValue_JTensor(
tensor.dataType(),
tensor.numDimensions(),
tensor.shape(),
toStringArray(byteArray, tensor.numElements(), dimensions));
}
break;
case INT32:
final IntBuffer intBuf = IntBuffer.allocate(tensor.numElements());
tensor.writeTo(intBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), intBuf.array());
break;
case INT64:
final LongBuffer longBuf = LongBuffer.allocate(tensor.numElements());
tensor.writeTo(longBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), longBuf.array());
break;
case FLOAT:
final FloatBuffer floatBuf = FloatBuffer.allocate(tensor.numElements());
tensor.writeTo(floatBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), floatBuf.array());
break;
case DOUBLE:
final DoubleBuffer doubleBuf = DoubleBuffer.allocate(tensor.numElements());
tensor.writeTo(doubleBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), doubleBuf.array());
break;
case BOOL:
final boolean[] array = new boolean[tensor.numElements()];
tensor.copyTo(array);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), array);
break;
default:
throw new IllegalStateException("Unsupported data type " + tensor.dataType());
}
} finally {
tensor.close();
}
return jt;
}
public synchronized static int[] glGetIntegerv(int value, int size) {
final IntBuffer buffer = IntBuffer.allocate(size);
GLES10.glGetIntegerv(value, buffer);
return buffer.array();
}
/**
* draw a sphere with a given radius.
* TODO expose quality parameters?
* TODO generate a sphere once as a model, return that.
* See https://www.gamedev.net/forums/topic/537269-procedural-sphere-creation/4469427/
* @param gl2
* @param radius
*/
static public void drawSphere(GL2 gl2,double radius) {
int width = 32;
int height = 16;
double theta, phi;
int i, j, t;
int nvec = (height-2)* width + 2;
int ntri = (height-2)*(width-1)*2;
FloatBuffer vertices = FloatBuffer.allocate(nvec * 3);
IntBuffer indexes = IntBuffer.allocate(ntri * 3);
float [] dat = vertices.array();
int [] idx = indexes.array();
for( t=0, j=1; j<height-1; j++ ) {
for(i=0; i<width; i++ ) {
theta = (double)(j)/(double)(height-1) * Math.PI;
phi = (double)(i)/(double)(width-1 ) * Math.PI*2;
dat[t++] = (float)( Math.sin(theta) * Math.cos(phi));
dat[t++] = (float)( Math.cos(theta));
dat[t++] = (float)(-Math.sin(theta) * Math.sin(phi));
}
}
dat[t++]= 0;
dat[t++]= 1;
dat[t++]= 0;
dat[t++]= 0;
dat[t++]=-1;
dat[t++]= 0;
for( t=0, j=0; j<height-3; j++ ) {
for( i=0; i<width-1; i++ ) {
idx[t++] = (j )*width + i ;
idx[t++] = (j+1)*width + i+1;
idx[t++] = (j )*width + i+1;
idx[t++] = (j )*width + i ;
idx[t++] = (j+1)*width + i ;
idx[t++] = (j+1)*width + i+1;
}
}
for( i=0; i<width-1; i++ ) {
idx[t++] = (height-2)*width;
idx[t++] = i;
idx[t++] = i+1;
idx[t++] = (height-2)*width+1;
idx[t++] = (height-3)*width + i+1;
idx[t++] = (height-3)*width + i;
}
int NUM_BUFFERS=1;
int[] VBO = new int[NUM_BUFFERS];
gl2.glGenBuffers(NUM_BUFFERS, VBO, 0);
gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[0]);
// Write out vertex buffer to the currently bound VBO.
int s=(Float.SIZE/8); // bits per float / bits per byte = bytes per float
gl2.glBufferData(GL2.GL_ARRAY_BUFFER, dat.length*s, vertices, GL2.GL_STATIC_DRAW);
gl2.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl2.glVertexPointer(3,GL2.GL_FLOAT,0,0);
gl2.glEnableClientState(GL2.GL_NORMAL_ARRAY);
gl2.glNormalPointer(GL2.GL_FLOAT,0,0);
gl2.glPushMatrix();
gl2.glScaled(radius,radius,radius);
gl2.glDrawElements(GL2.GL_TRIANGLES, ntri*3, GL2.GL_UNSIGNED_INT, indexes );
gl2.glPopMatrix();
gl2.glDisableClientState(GL2.GL_NORMAL_ARRAY);
gl2.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl2.glDeleteBuffers(NUM_BUFFERS, VBO, 0);
}