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:Main.java

/**
 * @param color Color of the region//  www .  ja v a2s.  c  o m
 * @param x X coordinate of the region
 * @param y Y coordinate of the region
 * @param bmp Source bitmap
 * */
static void fillRigeon(int color, int x, int y, Bitmap bmp) {

    for (int i = x * STEP; i < (x + 1) * STEP; i++)
        for (int j = y * STEP; j < (y + 1) * STEP; j++) {
            bmp.setPixel(i, j, color);
        }
}

From source file:Main.java

/**
 * Fill the hole in the image (version 2)
 *//*from ww w  .java  2s .co m*/
public static void fillHole2(Bitmap bmp) {

    for (int j = 1; j < bmp.getWidth(); j++) {
        for (int i = 1; i < bmp.getHeight() - 1; i++) {
            if ((bmp.getPixel(i, j) == Color.TRANSPARENT) && (bmp.getPixel(i + 1, j) != Color.TRANSPARENT)) {
                bmp.setPixel(i, j, bmp.getPixel(i + 1, j)); // set to the next-below pixel
            }
        }
    }
}

From source file:Main.java

/**
 * Fill the hole in the image (version 1)
 *///from  ww  w .j ava2s  .  com
public static void fillHole1(Bitmap bmp) {

    for (int j = 1; j < bmp.getWidth() - 1; j++) {
        for (int i = 1; i < bmp.getHeight(); i++) {
            if ((bmp.getPixel(i, j) == Color.TRANSPARENT) && (bmp.getPixel(i, j + 1) != Color.TRANSPARENT)) {
                bmp.setPixel(i, j, bmp.getPixel(i, j + 1)); // set to the right-next pixel
            }
        }
    }
}

From source file:org.odk.collect.android.utilities.QRCodeUtils.java

public static Bitmap generateQRBitMap(Collection<String> keys) {
    String content;/*from  ww  w  .  jav  a2  s .  c o m*/
    try {
        SharedPreferencesUtils obj = new SharedPreferencesUtils();
        content = obj.getJSONFromPreferences(keys);
        String compressedData = CompressionUtils.compress(content);

        //Maximum capacity for QR Codes is 4,296 characters (Alphanumeric)
        if (compressedData.length() > 4000) {
            ToastUtils.showLongToast(Collect.getInstance().getString(R.string.encoding_max_limit));
            Timber.e(Collect.getInstance().getString(R.string.encoding_max_limit));
            return null;
        }

        Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(compressedData, BarcodeFormat.QR_CODE, 400, 400, hints);

        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();

        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
            }
        }
        return bmp;
    } catch (WriterException | IOException | JSONException e) {
        Timber.e(e);
    }
    return null;
}

From source file:Main.java

public static Bitmap doColorFilter(Bitmap src, double red, double green, double blue) {
    int w = src.getWidth();
    int h = src.getHeight();

    Bitmap bmOut = Bitmap.createBitmap(w, h, src.getConfig());
    int A, R, G, B, pixel;
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            pixel = src.getPixel(x, y);//  w w w.j  a  v  a  2 s.  c o  m
            A = Color.alpha(pixel);
            R = (int) (Color.red(pixel) * red);
            G = (int) (Color.green(pixel) * green);
            B = (int) (Color.blue(pixel) * blue);
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

From source file:com.example.georg.theupub.ProfileActivity.java

public static Bitmap toBitmap(BitMatrix matrix) {
    int height = matrix.getHeight();
    int width = matrix.getWidth();
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
        }/*w  w  w  . j a v a2 s. co m*/
    }
    return bmp;
}

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

/**
 * @Deprecated use {@link #generateQrCodeFile(Context, String, int, int)} to avoid bitmap
 * management./*from  ww  w .  j a v  a  2s  .c o m*/
 */
@Deprecated
public static Observable<Bitmap> generateQrCode(String content, int width, int height) {
    return Observable.fromEmitter(emitter -> {
        MultiFormatWriter writer = new MultiFormatWriter();
        try {
            BitMatrix bm = writer.encode(content, BarcodeFormat.QR_CODE, QR_CODE_LENGTH, QR_CODE_LENGTH,
                    Collections.singletonMap(EncodeHintType.MARGIN, 0));
            Bitmap bitmap = 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++) {
                    bitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE);
                }
            }
            emitter.onNext(Bitmap.createScaledBitmap(bitmap, width, height, true));
            emitter.onCompleted();
        } catch (WriterException e) {
            emitter.onError(e);
        }
    }, Emitter.BackpressureMode.BUFFER);
}

From source file:Main.java

public static Bitmap subtract(Bitmap bitmap1, Bitmap bitmap2) {
    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 = Math.abs(value2 - value1);
            //                resultValue = (resultValue>255/3)?resultValue:0;
            result.setPixel(x, y, Color.rgb(resultValue, resultValue, resultValue));
        }//from   w w  w.j  av  a  2  s.com
    //        grayBitmap1.recycle();
    //        grayBitmap2.recycle();
    return result;
}

From source file:Main.java

public static void invertImage(Bitmap b) {
    int A, R, G, B;
    int pixelColor;
    int height = b.getHeight();
    int width = b.getWidth();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            pixelColor = b.getPixel(x, y);
            A = Color.alpha(pixelColor);

            R = 255 - Color.red(pixelColor);
            G = 255 - Color.green(pixelColor);
            B = 255 - Color.blue(pixelColor);

            b.setPixel(x, y, Color.argb(A, R, G, B));
        }/*from   w w w  .j  a va  2  s. c  o m*/
    }
}

From source file:Main.java

public static Bitmap multiply(Bitmap bitmap1, Bitmap bitmap2) {
    double alpha = 0.1;
    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((value2 / 255.0) * (value1 / 255.0)) * 255);
            //                resultValue = (resultValue>255/2)?255:0;
            result.setPixel(x, y, Color.rgb(resultValue, resultValue, resultValue));
        }//from  w  ww .j  av  a2 s .co m
    //        grayBitmap1.recycle();
    //        grayBitmap2.recycle();
    return result;
}