Java Code Examples for android.graphics.Bitmap.setPixel()

The following are Jave code examples for showing how to use setPixel() of the android.graphics.Bitmap class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: mDL-ILP   File: QRUtils.java   View Source Code Vote up 6 votes
public static Bitmap getQR(final byte[] content, final int size) {
    final QRCodeWriter writer = new QRCodeWriter();
    try {
        final Map<EncodeHintType, Object> encodingHints = new HashMap<>();
        encodingHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        encodingHints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

        final BitMatrix encoding = writer.encode(Utils.makeQR(content), BarcodeFormat.QR_CODE, size, size, encodingHints);
        final Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                bitmap.setPixel(i, j, encoding.get(i, j) ? COLOR_DARK : COLOR_LIGHT);
            }
        }

        return bitmap;
    } catch (WriterException e) {
        Log.e("QRUtils", "Failed to get QR code", e);
    }
    return null;
}
 
Example 2
Project: GitHub   File: SlowGreyScalePostprocessor.java   View Source Code Vote up 6 votes
@Override
public void process(Bitmap bitmap) {
  final int w = bitmap.getWidth();
  final int h = bitmap.getHeight();

  for (int x = 0; x < w; x++) {
    for (int y = 0; y < h; y++) {
      /*
       * Using {@link Bitmap#getPixel} when iterating about an entire bitmap is inefficient as it
       * causes many JNI calls. This is only done here to provide a comparison to
       * {@link FasterGreyScalePostprocessor}.
       */
      final int color = bitmap.getPixel(x, y);
      bitmap.setPixel(x, y, SlowGreyScalePostprocessor.getGreyColor(color));
    }
  }
}
 
Example 3
Project: FriskyImage   File: FriskyImageProperty.java   View Source Code Vote up 6 votes
public static    Bitmap FriskyBlackandWhite(Bitmap src){

        Bitmap dest = Bitmap.createBitmap(
                src.getWidth(), src.getHeight(), src.getConfig());

        for(int x = 0; x < src.getWidth(); x++){
            for(int y = 0; y < src.getHeight(); y++){
                int pixelColor = src.getPixel(x, y);
                int pixelAlpha = Color.alpha(pixelColor);
                int pixelRed = Color.red(pixelColor);
                int pixelGreen = Color.green(pixelColor);
                int pixelBlue = Color.blue(pixelColor);

                int pixelBW = (pixelRed + pixelGreen + pixelBlue)/3;
                int newPixel = Color.argb(
                        pixelAlpha, pixelBW, pixelBW, pixelBW);

                dest.setPixel(x, y, newPixel);
            }
        }
        return dest;
    }
 
Example 4
Project: TextView_CustomEdit_CustomColor   File: ColorPickerPreference.java   View Source Code Vote up 6 votes
private Bitmap getPreviewBitmap() {
	int d = (int) (mDensity * 31); //30dip
	int color = mValue;
	Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888);
	int w = bm.getWidth();
	int h = bm.getHeight();
	int c = color;
	for (int i = 0; i < w; i++) {
		for (int j = i; j < h; j++) {
			c = (i <= 1 || j <= 1 || i >= w-2 || j >= h-2) ? Color.GRAY : color;
			bm.setPixel(i, j, c);
			if (i != j) {
				bm.setPixel(j, i, c);
			}
		}
	}

	return bm;
}
 
Example 5
Project: PeSanKita-android   File: QrCode.java   View Source Code Vote up 6 votes
public static @NonNull Bitmap create(String data) {
  try {
    BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, 512, 512);
    Bitmap    bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);

    for (int y = 0; y < result.getHeight(); y++) {
      for (int x = 0; x < result.getWidth(); x++) {
        if (result.get(x, y)) {
          bitmap.setPixel(x, y, Color.BLACK);
        }
      }
    }

    return bitmap;
  } catch (WriterException e) {
    Log.w(TAG, e);
    return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
  }
}
 
Example 6
Project: Ocr-android   File: BitmapUtils.java   View Source Code Vote up 6 votes
/**
 * 实现对图像进行二值化处理
 *
 * @param origin bitmap
 * @return Bitmap
 */
public static Bitmap turnBinary(Bitmap origin) {
    int width = origin.getWidth();
    int height = origin.getHeight();
    Bitmap bitmap = origin.copy(Bitmap.Config.ARGB_8888, true);
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            int col = bitmap.getPixel(i, j);
            int alpha = col & 0xFF000000;
            int red = (col & 0x00FF0000) >> 16;
            int green = (col & 0x0000FF00) >> 8;
            int blue = (col & 0x000000FF);
            int gray = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
            if (gray <= 95) {
                gray = 0;
            } else {
                gray = 255;
            }
            int newColor = alpha | (gray << 16) | (gray << 8) | gray;
            bitmap.setPixel(i, j, newColor);
        }
    }
    return bitmap;
}
 
Example 7
Project: GravityBox   File: ColorPickerPreference.java   View Source Code Vote up 6 votes
private Bitmap getPreviewBitmap() {
	int d = (int) (mDensity * 31); //30dip
	int color = mValue;
	Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888);
	int w = bm.getWidth();
	int h = bm.getHeight();
	int c = color;
	for (int i = 0; i < w; i++) {
		for (int j = i; j < h; j++) {
			c = (i <= 1 || j <= 1 || i >= w-2 || j >= h-2) ? Color.GRAY : color;
			bm.setPixel(i, j, c);
			if (i != j) {
				bm.setPixel(j, i, c);
			}
		}
	}

	return bm;
}
 
Example 8
Project: phonk   File: PMedia.java   View Source Code Vote up 6 votes
public Bitmap generateQRCode(String text) {
    Bitmap bmp = null;

    Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // H = 30% damage

    int size = 256;

    BitMatrix bitMatrix = null;
    try {
        bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hintMap);

        int width = bitMatrix.getWidth();
        bmp = Bitmap.createBitmap(width, width, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < width; y++) {
                bmp.setPixel(y, x, bitMatrix.get(x, y) == true ? Color.BLACK : Color.WHITE);
            }
        }
    } catch (WriterException e) {
        e.printStackTrace();
    }

    return bmp;
}
 
Example 9
Project: Cable-Android   File: QrCode.java   View Source Code Vote up 6 votes
public static @NonNull Bitmap create(String data) {
  try {
    BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, 512, 512);
    Bitmap    bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);

    for (int y = 0; y < result.getHeight(); y++) {
      for (int x = 0; x < result.getWidth(); x++) {
        if (result.get(x, y)) {
          bitmap.setPixel(x, y, Color.BLACK);
        }
      }
    }

    return bitmap;
  } catch (WriterException e) {
    Log.w(TAG, e);
    return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
  }
}
 
Example 10
Project: ZxingScanner   File: QRCodeUtil.java   View Source Code Vote up 6 votes
public static Bitmap getQRcodeBitmap(String msg, int width, int height, int margin) {
    QRCodeWriter qw = new QRCodeWriter();

    try {
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, margin);
        BitMatrix matrix = qw.encode(msg, BarcodeFormat.QR_CODE, width, height, hints);
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < width; y++) {
                bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
            }
        }
        return bmp;
    } catch (WriterException e) {
        e.printStackTrace();
    }

    return null;
}
 
Example 11
Project: leoapp-sources   File: QRWriteTask.java   View Source Code Vote up 6 votes
private Bitmap createNewQR(String s) {
    MultiFormatWriter mFW = new MultiFormatWriter();
    Bitmap bM = null;

    int px = (int) GraphicUtils.dpToPx(250);

    try {
        BitMatrix bitM = mFW.encode(s, BarcodeFormat.QR_CODE, px, px);
        bM = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
        for (int i = 0; i < px; i++) {
            for (int j = 0; j < px; j++) {
                int c = (bitM.get(i, j)) ? Color.BLACK : Color.WHITE;
                bM.setPixel(i, j, c);
            }
        }
    } catch (WriterException e) {
        Utils.logError(e);
    }
    return bM;
}
 
Example 12
Project: IOSDialog   File: IOSDialog.java   View Source Code Vote up 5 votes
private Drawable adjust(Drawable d) {
    int to = Color.RED;

    //Need to copy to ensure that the bitmap is mutable.
    Bitmap src = ((BitmapDrawable) d).getBitmap();
    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for(int x = 0;x < bitmap.getWidth();x++)
        for(int y = 0;y < bitmap.getHeight();y++)
            if(match(bitmap.getPixel(x, y)))
                bitmap.setPixel(x, y, to);

    return new BitmapDrawable(bitmap);
}
 
Example 13
Project: IOSDialog   File: IOSDialog.java   View Source Code Vote up 5 votes
private Bitmap adjust(Bitmap src) {
    int to = Color.RED;

    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for(int x = 0;x < bitmap.getWidth();x++)
        for(int y = 0;y < bitmap.getHeight();y++)
            if(match(bitmap.getPixel(x, y)))
                bitmap.setPixel(x, y, to);

    return bitmap;
}
 
Example 14
Project: AwesomeQRCode   File: AwesomeQRCode.java   View Source Code Vote up 5 votes
private static void binarize(Bitmap bitmap, int threshold) {
    int r, g, b;
    for (int y = 0; y < bitmap.getHeight(); y++) {
        for (int x = 0; x < bitmap.getHeight(); x++) {
            int color = bitmap.getPixel(x, y);
            r = (color >> 16) & 0xFF;
            g = (color >> 8) & 0xFF;
            b = color & 0xFF;
            float sum = 0.30f * r + 0.59f * g + 0.11f * b;
            bitmap.setPixel(x, y, sum > threshold ? Color.WHITE : Color.BLACK);
        }
    }
}
 
Example 15
Project: PlusGram   File: RgbSelectorView.java   View Source Code Vote up 5 votes
private void setPreviewImage()
{
	Bitmap preview = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
	preview.setPixel(0, 0, getColor());
	
	imgPreview.setImageBitmap(preview);
}
 
Example 16
Project: xwallet   File: ReceiveFragment.java   View Source Code Vote up 5 votes
/**
 *
 * @param amount
 */
public void generateQRCode(String amount) {

    String coinName = getBaseActivity().getLastSyncedMessage().getCoinName();

    String uriStr = coinName + ":" + _addr;
    if (amount != null && !amount.isEmpty()) {
        uriStr += "?amount="+amount;
    }

    try {
        QRCodeWriter writer = new QRCodeWriter();

        BitMatrix bitMatrix = writer.encode(uriStr, BarcodeFormat.QR_CODE, 512, 512);
        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);
            }
        }

        _receiveFragmentView.getQrImg().setImageBitmap(bmp);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}
 
Example 17
Project: Ocr-android   File: BitmapUtils.java   View Source Code Vote up 5 votes
/**
 * 实现对图像进行线性灰度化处理
 *
 * @param origin bitmap
 * @return Bitmap
 */
public static Bitmap lineGray(Bitmap origin) {
    int width = origin.getWidth();
    int height = origin.getHeight();
    Bitmap bitmap = origin.copy(Bitmap.Config.ARGB_8888, true);
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            int col = origin.getPixel(i, j);
            int alpha = col & 0xFF000000;
            int red = (col & 0x00FF0000) >> 16;
            int green = (col & 0x0000FF00) >> 8;
            int blue = (col & 0x000000FF);
            red = (int) (1.1 * red + 30);
            green = (int) (1.1 * green + 30);
            blue = (int) (1.1 * blue + 30);
            if (red >= 255) {
                red = 255;
            }
            if (green >= 255) {
                green = 255;
            }
            if (blue >= 255) {
                blue = 255;
            }
            int newColor = alpha | (red << 16) | (green << 8) | blue;
            bitmap.setPixel(i, j, newColor);
        }
    }
    return bitmap;
}
 
Example 18
Project: mpeg-encoder   File: GLToolsAndroidTest.java   View Source Code Vote up 4 votes
/**
 * Test for {@link GLTools#drawFrame(Buffer, int, int, int)}.
 * @throws Exception by some fails
 */
@Test
public final void testDrawFrame() throws Exception {
    final EGLDisplay eglDisplay = GLTools.newDisplay();
    final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true);
    final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig);
    final EGLSurface eglSurface =
            GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE);

    GLTools.makeCurrent(eglDisplay, eglSurface, eglContext);

    final int[] attrs = new int[5];
    GLTools.newShader(attrs);
    final int texture = GLTools.newTexture(TEXTURE_LEVEL);

    // 1-st pass
    Bitmap bitmap = Bitmap.createBitmap(FRAME_SIZE, FRAME_SIZE, Bitmap.Config.RGB_565);
    bitmap.setPixel(0, 0, Color.RED); bitmap.setPixel(1, 0, Color.GREEN);
    bitmap.setPixel(0, 1, Color.BLUE); bitmap.setPixel(1, 1, Color.YELLOW);
    ByteBuffer buffer = ByteBuffer.allocate(FRAME_SIZE * FRAME_SIZE * 2);
    bitmap.copyPixelsToBuffer(buffer); bitmap.recycle();

    GLTools.makeCurrent(eglDisplay, eglSurface, eglContext);
    GLTools.drawFrame(buffer, FRAME_SIZE, FRAME_SIZE, 0); buffer.clear();
    //GLTools.swapBuffers(eglDisplay, eglSurface);

    buffer = ByteBuffer.allocateDirect(FRAME_SIZE * FRAME_SIZE * 4);
    GLES20.glReadPixels(0, 0, FRAME_SIZE, FRAME_SIZE,
            GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);

    bitmap = Bitmap.createBitmap(FRAME_SIZE, FRAME_SIZE, Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(buffer); buffer.clear(); //bitmap.eraseColor(Color.BLACK);

    Assert.assertArrayEquals(new int[]{bitmap.getPixel(0, 0)}, new int[]{Color.BLUE});
    Assert.assertArrayEquals(new int[]{bitmap.getPixel(1, 0)}, new int[]{Color.YELLOW});
    Assert.assertArrayEquals(new int[]{bitmap.getPixel(0, 1)}, new int[]{Color.RED});
    Assert.assertArrayEquals(new int[]{bitmap.getPixel(1, 1)}, new int[]{Color.GREEN});
    bitmap.recycle();

    // 2-nd pass
    bitmap = Bitmap.createBitmap(FRAME_SIZE, FRAME_SIZE, Bitmap.Config.RGB_565);
    bitmap.setPixel(0, 0, Color.YELLOW); bitmap.setPixel(1, 0, Color.BLUE);
    bitmap.setPixel(0, 1, Color.GREEN); bitmap.setPixel(1, 1, Color.RED);
    buffer = ByteBuffer.allocate(FRAME_SIZE * FRAME_SIZE * 2);
    bitmap.copyPixelsToBuffer(buffer); bitmap.recycle();

    GLTools.makeCurrent(eglDisplay, eglSurface, eglContext);
    GLTools.drawFrame(buffer, FRAME_SIZE, FRAME_SIZE, 0); buffer.clear();
    //GLTools.swapBuffers(eglDisplay, eglSurface);

    buffer = ByteBuffer.allocateDirect(FRAME_SIZE * FRAME_SIZE * 4);
    GLES20.glReadPixels(0, 0, FRAME_SIZE, FRAME_SIZE,
            GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);

    bitmap = Bitmap.createBitmap(FRAME_SIZE, FRAME_SIZE, Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(buffer); buffer.clear(); //bitmap.eraseColor(Color.BLACK);

    Assert.assertArrayEquals(new int[]{bitmap.getPixel(0, 0)}, new int[]{Color.GREEN});
    Assert.assertArrayEquals(new int[]{bitmap.getPixel(1, 0)}, new int[]{Color.RED});
    Assert.assertArrayEquals(new int[]{bitmap.getPixel(0, 1)}, new int[]{Color.YELLOW});
    Assert.assertArrayEquals(new int[]{bitmap.getPixel(1, 1)}, new int[]{Color.BLUE});
    bitmap.recycle();

    GLTools.closeTexture(texture, TEXTURE_LEVEL);
    GLTools.closeShader(attrs);

    GLTools.closeSurface(eglDisplay, eglSurface);
    GLTools.closeContext(eglDisplay, eglContext);
    GLTools.closeDisplay(eglDisplay);
}
 
Example 19
Project: ESPD   File: TextureCache.java   View Source Code Vote up 4 votes
public static SmartTexture createGradient(int... colors) {

		final String key = "" + colors;

		if (all.containsKey(key)) {

			return all.get(key);

		} else {

			Bitmap bmp = Bitmap.createBitmap(colors.length, 1, Bitmap.Config.ARGB_8888);
			for (int i = 0; i < colors.length; i++) {
				bmp.setPixel(i, 0, colors[i]);
			}
			SmartTexture tx = new SmartTexture(bmp);

			tx.filter(Texture.LINEAR, Texture.LINEAR);
			tx.wrap(Texture.CLAMP, Texture.CLAMP);

			all.put(key, tx);
			return tx;
		}

	}
 
Example 20
Project: UNIST-pixel-dungeon   File: TextureCache.java   View Source Code Vote up 4 votes
public static SmartTexture createGradient( int... colors ) {
	
	final String key = "" + colors;
	
	if (all.containsKey( key )) {
		
		return all.get( key );
		
	} else {
	
		Bitmap bmp = Bitmap.createBitmap( colors.length, 1, Bitmap.Config.ARGB_8888 );
		for (int i=0; i < colors.length; i++) {
			bmp.setPixel( i, 0, colors[i] );
		}
		SmartTexture tx = new SmartTexture( bmp );

		tx.filter( Texture.LINEAR, Texture.LINEAR );
		tx.wrap( Texture.CLAMP, Texture.CLAMP );

		all.put( key, tx );
		return tx;
	}
	
}