Java Code Examples for java.nio.IntBuffer#array()

The following examples show how to use java.nio.IntBuffer#array() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OffscreenImage.java    From EZFilter with MIT License 7 votes vote down vote up
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;
}
 
Example 2
Source File: MovieSegment.java    From PhotoMovie with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: Identicon.java    From masterpassword with GNU General Public License v3.0 6 votes vote down vote up
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]);
}
 
Example 4
Source File: PixelBuffer.java    From In77Camera with MIT License 6 votes vote down vote up
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));
}
 
Example 5
Source File: OpenGlUtil.java    From SimpleVideoEditor with Apache License 2.0 6 votes vote down vote up
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"));
}
 
Example 6
Source File: PixelBuffer.java    From SimpleVideoEditor with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example 7
Source File: PixelBuffer.java    From Fatigue-Detection with MIT License 6 votes vote down vote up
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));
}
 
Example 8
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example 9
Source File: HashUtil.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: IntTensorTypeSupport.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 11
Source File: MiscTools.java    From megabasterd with GNU General Public License v3.0 5 votes vote down vote up
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;
    }
}
 
Example 12
Source File: IntPointer.java    From tapir with MIT License 5 votes vote down vote up
/**
 * 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());
    }
}
 
Example 13
Source File: TensorUtil.java    From jpmml-tensorflow with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public int[] toIntArray(Tensor tensor){
	IntBuffer intBuffer = IntBuffer.allocate(tensor.numElements());

	tensor.writeTo(intBuffer);

	return intBuffer.array();
}
 
Example 14
Source File: BitmapOutput.java    From EZFilter with MIT License 5 votes vote down vote up
@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);
            }
        }
    }
 
Example 15
Source File: BlockChangeArray.java    From PacketWrapper with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 16
Source File: BitmapUtils.java    From In77Camera with MIT License 5 votes vote down vote up
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;
}
 
Example 17
Source File: EglBuffer.java    From HokoBlur with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 18
Source File: GPU.java    From DeviceInfo with Apache License 2.0 4 votes vote down vote up
public synchronized static int[] glGetIntegerv(int value, int size) {
    final IntBuffer buffer = IntBuffer.allocate(size);
    GLES10.glGetIntegerv(value, buffer);
    return buffer.array();
}
 
Example 19
Source File: PrimitiveSolids.java    From Robot-Overlord-App with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: JTensor.java    From zoltar with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}