Example usage for android.graphics Bitmap setPixel

List of usage examples for android.graphics Bitmap setPixel

Introduction

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

Prototype

public void setPixel(int x, int y, @ColorInt int color) 

Source Link

Document

Write the specified Color into the bitmap (assuming it is mutable) at the x,y coordinate.

Usage

From source file:us.rader.tapset.ShowQrCodeActivity.java

/**
 * Return the QR code image {@link Bitmap} for the given {@link Uri}
 * // w ww  . jav a 2  s.c o  m
 * @param uri
 *            the {@link Uri} to encode
 * 
 * @return the QR code {@link Bitmap}
 * 
 * @throws WriterException
 *             if an error occurs encoding the QR code
 */
private static Bitmap encodeQrCode(Uri uri) throws WriterException {

    QRCodeWriter writer = new QRCodeWriter();
    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    BitMatrix bitMatrix = writer.encode(uri.toString(), BarcodeFormat.QR_CODE, QR_DIMENSION, QR_DIMENSION,
            hints);
    Bitmap bitmap = Bitmap.createBitmap(QR_DIMENSION, QR_DIMENSION, Bitmap.Config.ARGB_8888);

    for (int y = 0; y < bitMatrix.getHeight(); ++y) {

        for (int x = 0; x < bitMatrix.getWidth(); ++x) {

            bitmap.setPixel(x, y, (bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE));

        }
    }

    return bitmap;

}

From source file:Main.java

public static Bitmap doInvert(Bitmap src) {
    Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
    int A, R, G, B;
    int pixelColor;
    int w = src.getWidth();
    int h = src.getHeight();
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            pixelColor = src.getPixel(x, y);
            A = Color.alpha(pixelColor);
            R = 255 - Color.red(pixelColor);
            G = 255 - Color.green(pixelColor);
            B = 255 - Color.blue(pixelColor);
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }/*from   w  w w . j a va 2 s. c o  m*/
    }
    return bmOut;
}

From source file:Main.java

public static Bitmap doGray(Bitmap src) {
    final double GS_RED = 0.299;
    final double GS_GREEN = 0.587;
    final double GS_BLUE = 0.114;

    Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
    int A, R, G, B, pixel;
    int w = src.getWidth();
    int h = src.getHeight();
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            pixel = src.getPixel(x, y);/*from w  ww . j ava  2 s. co m*/

            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

From source file:Main.java

public static Bitmap divide(Bitmap bitmap1, Bitmap bitmap2) {
    double alpha = Double.MIN_VALUE;
    int width = bitmap1.getWidth();
    int height = bitmap1.getHeight();
    Bitmap grayBitmap1 = toGrayScale(bitmap1);
    Bitmap grayBitmap2 = toGrayScale(bitmap2);
    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++) {
            int value1 = Color.blue(grayBitmap1.getPixel(x, y));
            int value2 = Color.blue(grayBitmap2.getPixel(x, y));
            //                int resultValue = (int) (Math.abs((Math.log((value1 + alpha)/(value2 + alpha))) * 1000));
            int resultValue = (int) ((Math.abs(Math.log((value1 + alpha) / (value2 + alpha)))) * 255);
            result.setPixel(x, y, Color.rgb(resultValue, resultValue, resultValue));
        }/*from  ww  w  .  ja  v  a2 s.c  o  m*/
    //        grayBitmap1.recycle();
    //        grayBitmap2.recycle();
    return result;
}

From source file:Main.java

public static Bitmap createIdenticon(byte[] hash, int background) {
    Bitmap identicon = Bitmap.createBitmap(5, 5, Bitmap.Config.ARGB_8888);

    float[] hsv = { Integer.valueOf(hash[3] + 128).floatValue() * 360.0f / 255.0f, 0.8f, 0.8f };
    int foreground = Color.HSVToColor(hsv);

    for (int x = 0; x < 5; x++) {
        //Enforce horizontal symmetry
        int i = x < 3 ? x : 4 - x;
        for (int y = 0; y < 5; y++) {
            int pixelColor;
            //toggle pixels based on bit being on/off
            if ((hash[i] >> y & 1) == 1)
                pixelColor = foreground;
            else//from  w  w  w .  j a  v a  2  s  . c o  m
                pixelColor = background;
            identicon.setPixel(x, y, pixelColor);
        }
    }

    Bitmap bmpWithBorder = Bitmap.createBitmap(48, 48, identicon.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(background);
    identicon = Bitmap.createScaledBitmap(identicon, 46, 46, false);
    canvas.drawBitmap(identicon, 1, 1, null);

    return bmpWithBorder;
}

From source file:com.github.piasy.rxqrcode.RxQrCode.java

public static Observable<File> generateQrCodeFile(Context context, String content, int width, int height) {
    return Observable.fromEmitter(emitter -> {
        MultiFormatWriter writer = new MultiFormatWriter();
        Bitmap origin = null;
        Bitmap scaled = null;/*from  w w w  . j a v a 2  s  . c o  m*/
        try {
            BitMatrix bm = writer.encode(content, BarcodeFormat.QR_CODE, QR_CODE_LENGTH, QR_CODE_LENGTH,
                    Collections.singletonMap(EncodeHintType.MARGIN, 0));
            origin = Bitmap.createBitmap(QR_CODE_LENGTH, QR_CODE_LENGTH, Bitmap.Config.ARGB_8888);

            for (int i = 0; i < QR_CODE_LENGTH; i++) {
                for (int j = 0; j < QR_CODE_LENGTH; j++) {
                    origin.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE);
                }
            }
            scaled = Bitmap.createScaledBitmap(origin, width, height, true);
            File dir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            if (dir == null) {
                emitter.onError(new IllegalStateException("external file system unavailable!"));
                return;
            }
            String fileName = "rx_qr_" + System.currentTimeMillis() + ".png";
            File localFile = new File(dir, fileName);

            FileOutputStream outputStream = new FileOutputStream(localFile);
            scaled.compress(Bitmap.CompressFormat.PNG, 85, outputStream);
            outputStream.flush();
            outputStream.close();

            emitter.onNext(localFile);
            emitter.onCompleted();
        } catch (WriterException | IOException e) {
            emitter.onError(e);
        } finally {
            if (origin != null) {
                origin.recycle();
            }
            if (scaled != null) {
                scaled.recycle();
            }
        }
    }, Emitter.BackpressureMode.BUFFER);
}

From source file:Main.java

public static Bitmap boost(Bitmap src, int type, float percent) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());

    int A, R, G, B;
    int pixel;//from ww  w .  j  a  va  2 s.  c  om

    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            if (type == 1) {
                R = (int) (R * (1 + percent));
                if (R > 255)
                    R = 255;
            } else if (type == 2) {
                G = (int) (G * (1 + percent));
                if (G > 255)
                    G = 255;
            } else if (type == 3) {
                B = (int) (B * (1 + percent));
                if (B > 255)
                    B = 255;
            }
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

From source file:io.jawg.osmcontributor.ui.utils.BitmapHandler.java

public static Bitmap hue(Bitmap bitmap, float hue) {
    Bitmap newBitmap = bitmap.copy(bitmap.getConfig(), true);
    final int width = newBitmap.getWidth();
    final int height = newBitmap.getHeight();
    float[] hsv = new float[3];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int pixel = newBitmap.getPixel(x, y);
            Color.colorToHSV(pixel, hsv);
            hsv[0] = hue;/*from   w w  w . j  a v a2 s.co m*/
            newBitmap.setPixel(x, y, Color.HSVToColor(Color.alpha(pixel), hsv));
        }
    }

    return newBitmap;
}

From source file:Main.java

public static Bitmap doSherpiaEffect(Bitmap src, int depth, double red, double green, double blue) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    final double GS_RED = 0.3;
    final double GS_GREEN = 0.59;
    final double GS_BLUE = 0.11;
    int A, R, G, B;
    int pixel;/*  w ww  . ja v  a  2  s. c om*/

    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            B = G = R = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);

            R += (depth * red);
            if (R > 255) {
                R = 255;
            }

            G += (depth * green);
            if (G > 255) {
                G = 255;
            }

            B += (depth * blue);
            if (B > 255) {
                B = 255;
            }

            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

From source file:org.disrupted.rumble.userinterface.activity.GroupDetailActivity.java

public void invite() {
    String buffer = group.getGroupBase64ID();
    int size = 200;
    Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    try {//  w  w w  .ja  v a  2 s .co  m
        BitMatrix bitMatrix = qrCodeWriter.encode(buffer, BarcodeFormat.QR_CODE, size, size, hintMap);
        Bitmap image = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

        if (image != null) {
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    image.setPixel(i, j, bitMatrix.get(i, j) ? Color.BLACK : Color.WHITE);
                }
            }
            Intent intent = new Intent(this, DisplayQRCode.class);
            intent.putExtra("EXTRA_GROUP_NAME", groupName);
            intent.putExtra("EXTRA_BUFFER", buffer);
            intent.putExtra("EXTRA_QRCODE", image);
            startActivity(intent);
            overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_open_exit);
        }
    } catch (WriterException ignore) {
    }
    //}
}