Example usage for android.graphics Bitmap copyPixelsFromBuffer

List of usage examples for android.graphics Bitmap copyPixelsFromBuffer

Introduction

In this page you can find the example usage for android.graphics Bitmap copyPixelsFromBuffer.

Prototype

public void copyPixelsFromBuffer(Buffer src) 

Source Link

Document

Copy the pixels from the buffer, beginning at the current position, overwriting the bitmap's pixels.

Usage

From source file:Main.java

public static Bitmap byteArrayToBitmap(int width, int height, byte[] byteArray) {
    // use Bitmap.Config.ARGB_8888 instead of type is OK
    Bitmap stitchBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    stitchBmp.copyPixelsFromBuffer(ByteBuffer.wrap(byteArray));
    return stitchBmp;
}

From source file:Main.java

public static void saveRgb2Bitmap(IntBuffer buf, String filePath, int width, int height) {
    final int[] pixelMirroredArray = new int[width * height];
    Log.d("TryOpenGL", "Creating " + filePath);
    BufferedOutputStream bos = null;
    try {//from   w  w w. ja v  a2  s  .c o m
        int[] pixelArray = buf.array();
        // rotate 180 deg with x axis because y is reversed
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                pixelMirroredArray[(height - i - 1) * width + j] = pixelArray[i * width + j];
            }
        }
        bos = new BufferedOutputStream(new FileOutputStream(filePath));
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bmp.copyPixelsFromBuffer(IntBuffer.wrap(pixelMirroredArray));
        bmp.compress(Bitmap.CompressFormat.JPEG, 90, bos);
        bmp.recycle();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static Bitmap getBitmapFromBuffer(Buffer src, int width) {
    if (null == src) {
        return null;
    }//w ww  .  j  av  a2s.  co m

    int height = getHeightFromWidth(width);
    if (-1 == height) {
        return null;
    }

    Bitmap bitmap = null;
    try {
        bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
        bitmap.copyPixelsFromBuffer(src);
    } catch (Exception e) {
        e.printStackTrace();
        bitmap = null;
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap fromYUV420P(byte[] yuv, int width, int height) {
    if (yuv == null) {
        Log.e(TAG, "yuv data==null");
        return null;
    }/*from w  w w . j  a v  a  2s  .  c  om*/
    if (yuv.length != width * height * 1.5) {
        Log.e(TAG, "yudData does not match the provided width and height");
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    int offsetY = 0;
    ShortBuffer buffer = ShortBuffer.allocate(width * height * 2);
    for (int line = 0; line < height; line++) {
        for (int col = 0; col < width; col++) {
            int y = yuv[offsetY++] & 0xFF;
            buffer.put((short) ((y >> 3) << 11 | (y >> 2) << 5 | (y >> 3)));
        }
    }
    bitmap.copyPixelsFromBuffer(buffer);
    return bitmap;
}

From source file:com.sien.cpshoot.screencapture.ScreenCaptureFragment.java

private void saveImage(ImageReader mImageReader, int mWidth, int mHeight) {
    if (mImageReader == null)
        return;//from w ww  .ja v  a 2s . c o  m

    Image image = mImageReader.acquireLatestImage();
    if (image == null)
        return;
    final Image.Plane[] planes = image.getPlanes();
    if (planes.length <= 0)
        return;

    final ByteBuffer buffer = planes[0].getBuffer();
    int offset = 0;
    int pixelStride = planes[0].getPixelStride();
    int rowStride = planes[0].getRowStride();
    int rowPadding = rowStride - pixelStride * mWidth;
    Bitmap bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.RGB_565);
    bitmap.copyPixelsFromBuffer(buffer);
    image.close();

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
    String strDate = dateFormat.format(new java.util.Date());
    String pathImage = Environment.getExternalStorageDirectory().getPath() + "/Pictures/";
    String nameImage = pathImage + strDate + ".png";
    if (bitmap != null) {
        try {
            File fileImage = new File(nameImage);
            if (!fileImage.exists()) {
                fileImage.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(fileImage);
            if (out != null) {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
                Toast.makeText(getActivity(), "get phone's screen succeed", Toast.LENGTH_SHORT).show();
                Intent media = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri contentUri = Uri.fromFile(fileImage);
                media.setData(contentUri);
                getActivity().sendBroadcast(media);

                //
                beginCrop(Uri.fromFile(new File(nameImage)));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.mozilla.gecko.gfx.GeckoSoftwareLayerClient.java

public void copyPixelsFromMultiTileLayer(Bitmap target) {
    Canvas c = new Canvas(target);
    ByteBuffer tileBuffer = mBuffer.slice();
    int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat) / 8;

    for (int y = 0; y < mBufferSize.height; y += TILE_SIZE.height) {
        for (int x = 0; x < mBufferSize.width; x += TILE_SIZE.width) {
            // Calculate tile size
            IntSize tileSize = new IntSize(Math.min(mBufferSize.width - x, TILE_SIZE.width),
                    Math.min(mBufferSize.height - y, TILE_SIZE.height));

            // Create a Bitmap from this tile
            Bitmap tile = Bitmap.createBitmap(tileSize.width, tileSize.height,
                    CairoUtils.cairoFormatTobitmapConfig(mFormat));
            tile.copyPixelsFromBuffer(tileBuffer.asIntBuffer());

            // Copy the tile to the master Bitmap and recycle it
            c.drawBitmap(tile, x, y, null);
            tile.recycle();// w w w .ja v  a 2s .c om

            // Progress the buffer to the next tile
            tileBuffer.position(tileSize.getArea() * bpp);
            tileBuffer = tileBuffer.slice();
        }
    }
}

From source file:Main.java

public static Bitmap convertToMutable(Bitmap srcBitmap, String cacheDirPath, String tempFileName) {
    try {/*from   w  w w. j  a v  a2s.c  o  m*/
        // this is the file going to use temporally to save the bytes.
        // This file will not be a image, it will store the raw image data.
        int index = tempFileName.lastIndexOf(".");
        if (index != -1)
            tempFileName = tempFileName.substring(0, index);
        File file = new File(cacheDirPath + File.separator + tempFileName + ".tmp");

        // Open an RandomAccessFile
        // Make sure you have added uses-permission
        // android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        // into AndroidManifest.xml file
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = srcBitmap.getWidth();
        int height = srcBitmap.getHeight();
        Config type = srcBitmap.getConfig();

        // Copy the byte to the file
        // Assume source bitmap loaded using options.inPreferredConfig =
        // Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, srcBitmap.getRowBytes() * height);
        srcBitmap.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        srcBitmap.recycle();
        System.gc();// try to force the bytes from the imgIn to be released

        // Create a new bitmap to load the bitmap again. Probably the memory
        // will be available.
        srcBitmap = Bitmap.createBitmap(width, height, type);
        map.position(0);
        // load it back from temporary
        srcBitmap.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

        // delete the temp file
        file.delete();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return srcBitmap;
}

From source file:org.mozilla.gecko.GeckoAppShell.java

public static void notifyScreenShot(final ByteBuffer data, final int tabId, final int width, final int height) {
    getHandler().post(new Runnable() {
        public void run() {
            Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
            b.copyPixelsFromBuffer(data);
            freeDirectBuffer(data);//from   w w  w .j  a  v a2s  . c om
            final Tab tab = Tabs.getInstance().getTab(tabId);
            GeckoApp.mAppContext.processThumbnail(tab, b, null);
        }
    });
}

From source file:com.gsbabil.antitaintdroid.UtilityFunctions.java

/**
 * Source:/*w ww.  j  a v  a 2  s. c om*/
 * http://stackoverflow.com/questions/4349075/bitmapfactory-decoderesource
 * -returns-a-mutable-bitmap-in-android-2-2-and-an-immu
 * 
 * Converts a immutable bitmap to a mutable bitmap. This operation doesn't
 * allocates more memory that there is already allocated.
 * 
 * @param imgIn
 *            - Source image. It will be released, and should not be used
 *            more
 * @return a copy of imgIn, but immutable.
 */
public static Bitmap convertBitmapToMutable(Bitmap imgIn) {
    try {
        // this is the file going to use temporally to save the bytes.
        // This file will not be a image, it will store the raw image data.
        File file = new File(MyApp.context.getFilesDir() + File.separator + "temp.tmp");

        // Open an RandomAccessFile
        // Make sure you have added uses-permission
        // android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        // into AndroidManifest.xml file
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = imgIn.getWidth();
        int height = imgIn.getHeight();
        Config type = imgIn.getConfig();

        // Copy the byte to the file
        // Assume source bitmap loaded using options.inPreferredConfig =
        // Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height);
        imgIn.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        imgIn.recycle();
        System.gc();// try to force the bytes from the imgIn to be released

        // Create a new bitmap to load the bitmap again. Probably the memory
        // will be available.
        imgIn = Bitmap.createBitmap(width, height, type);
        map.position(0);
        // load it back from temporary
        imgIn.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

        // delete the temporary file
        file.delete();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return imgIn;
}

From source file:com.example.psumaps.MapView.java

public static Bitmap convertToMutable(Bitmap imgIn) {
    try {/* w  w w  .java 2 s.  c om*/
        // this is the file going to use temporally to save the bytes.
        // This file will not be a image, it will store the raw image data.
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.tmp");

        // Open an RandomAccessFile
        // Make sure you have added uses-permission
        // android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        // into AndroidManifest.xml file
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = imgIn.getWidth();
        int height = imgIn.getHeight();
        Bitmap.Config type = imgIn.getConfig();

        // Copy the byte to the file
        // Assume source bitmap loaded using options.inPreferredConfig =
        // Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height);
        imgIn.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        imgIn.recycle();
        System.gc();// try to force the bytes from the imgIn to be released

        // Create a new bitmap to load the bitmap again. Probably the memory
        // will be available.
        imgIn = Bitmap.createBitmap(width, height, type);
        map.position(0);
        // load it back from temporary
        imgIn.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

        // delete the temp file
        file.delete();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return imgIn;
}