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

The following are Jave code examples for showing how to use setPixels() 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: zxing_qrcode_demo   File: PlanarYUVLuminanceSource.java   View Source Code Vote up 6 votes
public Bitmap renderCroppedGreyscaleBitmap() {
    int width = getWidth();
    int height = getHeight();
    int[] pixels = new int[width * height];
    byte[] yuv = yuvData;
    int inputOffset = top * dataWidth + left;

    for (int y = 0; y < height; y++) {
        int outputOffset = y * width;
        for (int x = 0; x < width; x++) {
            int grey = yuv[inputOffset + x] & 0xff;
            pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
        }
        inputOffset += dataWidth;
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example 2
Project: ZxingForAndroid   File: QRCodeEncoder.java   View Source Code Vote up 6 votes
/**
 * 同步创建指定前景色、指定背景色、带logo的二维码图片。该方法是耗时操作,请在子线程中调用。
 *
 * @param content         要生成的二维码图片内容
 * @param size            图片宽高,单位为px
 * @param foregroundColor 二维码图片的前景色
 * @param backgroundColor 二维码图片的背景色
 * @param logo            二维码图片的logo
 */
public static Bitmap syncEncodeQRCode(String content, int size, int foregroundColor, int backgroundColor, Bitmap logo) {
    try {
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, HINTS);
        int[] pixels = new int[size * size];
        for (int y = 0; y < size; y++) {
            for (int x = 0; x < size; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * size + x] = foregroundColor;
                } else {
                    pixels[y * size + x] = backgroundColor;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
        return addLogoToQRCode(bitmap, logo);
    } catch (Exception e) {
        return null;
    }
}
 
Example 3
Project: GitHub   File: EncodingHandler.java   View Source Code Vote up 6 votes
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
	Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
	BitMatrix matrix = new MultiFormatWriter().encode(str,
			BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
	int width = matrix.getWidth();
	int height = matrix.getHeight();
	int[] pixels = new int[width * height];
	
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			if (matrix.get(x, y)) {
				pixels[y * width + x] = BLACK;
			}
		}
	}
	Bitmap bitmap = Bitmap.createBitmap(width, height,
			Bitmap.Config.ARGB_8888);
	bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
	return bitmap;
}
 
Example 4
Project: GitHub   File: PlanarYUVLuminanceSource.java   View Source Code Vote up 6 votes
public Bitmap renderCroppedGreyscaleBitmap() {
  int width = getWidth();
  int height = getHeight();
  int[] pixels = new int[width * height];
  byte[] yuv = yuvData;
  int inputOffset = top * dataWidth + left;

  for (int y = 0; y < height; y++) {
    int outputOffset = y * width;
    for (int x = 0; x < width; x++) {
      int grey = yuv[inputOffset + x] & 0xff;
      pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
    }
    inputOffset += dataWidth;
  }

  Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
  return bitmap;
}
 
Example 5
Project: MeiLa_GNN   File: EncodingHandler.java   View Source Code Vote up 6 votes
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = BLACK;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example 6
Project: GitHub   File: PlatformBitmapFactory.java   View Source Code Vote up 6 votes
/**
 * Creates a bitmap with the specified width and height.  Its initial density is
 * determined from the given DisplayMetrics.
 *
 * @param display Display metrics for the display this bitmap will be drawn on
 * @param colors The colors to write to the bitmap
 * @param offset The index of the first color to read from colors[]
 * @param stride The number of colors in pixels[] to skip between rows.
 * @param width The width of the bitmap
 * @param height The height of the bitmap
 * @param config The bitmap config to create
 * @param callerContext the Tag to track who create the Bitmap
 * @return a reference to the bitmap
 * @throws IllegalArgumentException if the width or height are <= 0
 * @throws TooManyBitmapsException if the pool is full
 * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
 */
public CloseableReference<Bitmap> createBitmap(
    DisplayMetrics display,
    int[] colors,
    int offset,
    int stride,
    int width,
    int height,
    Bitmap.Config config,
    @Nullable Object callerContext) {
  CloseableReference<Bitmap> bitmapRef = createBitmap(
      display,
      width,
      height,
      config,
      callerContext);
  Bitmap bitmap = bitmapRef.get();
  bitmap.setPixels(colors, offset, stride, 0, 0, width, height);
  return bitmapRef;
}
 
Example 7
Project: RLibrary   File: RCode.java   View Source Code Vote up 6 votes
/**
 * 同步创建指定前景色、指定背景色、带logo的二维码图片。该方法是耗时操作,请在子线程中调用。
 *
 * @param content         要生成的二维码图片内容
 * @param size            图片宽高,单位为px
 * @param foregroundColor 二维码图片的前景色
 * @param backgroundColor 二维码图片的背景色
 * @param logo            二维码图片的logo
 */
public static Bitmap syncEncodeQRCode(String content, int size, int foregroundColor, int backgroundColor, Bitmap logo) {
    try {
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, HINTS);
        int[] pixels = new int[size * size];
        for (int y = 0; y < size; y++) {
            for (int x = 0; x < size; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * size + x] = foregroundColor;
                } else {
                    pixels[y * size + x] = backgroundColor;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
        return addLogoToQRCode(bitmap, logo);
    } catch (Exception e) {
        return null;
    }
}
 
Example 8
Project: Zxing   File: PlanarYUVLuminanceSource.java   View Source Code Vote up 6 votes
public Bitmap renderCroppedGreyscaleBitmap() {
	int width = getWidth();
	int height = getHeight();
	int[] pixels = new int[width * height];
	byte[] yuv = yuvData;
	int inputOffset = top * dataWidth + left;

	for (int y = 0; y < height; y++) {
		int outputOffset = y * width;
		for (int x = 0; x < width; x++) {
			int grey = yuv[inputOffset + x] & 0xff;
			pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
		}
		inputOffset += dataWidth;
	}

	Bitmap bitmap = Bitmap.createBitmap(width, height,
			Bitmap.Config.ARGB_8888);
	bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
	return bitmap;
}
 
Example 9
Project: tvConnect_android   File: EncodingHandler.java   View Source Code Vote up 6 votes
public static Bitmap createQRCode(String str, int size) throws WriterException {
	Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
	hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
	BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, size, size);
	int width = matrix.getWidth();
	int height = matrix.getHeight();
	int[] pixels = new int[width * height];
	for(int x = 0; x < width; x ++){
		for(int y = 0; y < height; y ++){
			if(matrix.get(x, y)){
				pixels[y * width + x] = BLACK;
			}
		}
	}
	Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
	bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
	return bitmap;
}
 
Example 10
Project: MeiLa_GNN   File: PlanarYUVLuminanceSource.java   View Source Code Vote up 6 votes
public Bitmap renderCroppedGreyscaleBitmap() {
    int width = getWidth();
    int height = getHeight();
    int[] pixels = new int[width * height];
    byte[] yuv = yuvData;
    int inputOffset = top * dataWidth + left;

    for (int y = 0; y < height; y++) {
        int outputOffset = y * width;
        for (int x = 0; x < width; x++) {
            int grey = yuv[inputOffset + x] & 0xff;
            pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
        }
        inputOffset += dataWidth;
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example 11
Project: android-mrz-reader   File: PlanarYUVLuminanceSource.java   View Source Code Vote up 6 votes
public Bitmap renderCroppedGreyscaleBitmap() {
  int width = getWidth();
  int height = getHeight();
  int[] pixels = new int[width * height];
  byte[] yuv = yuvData;
  int inputOffset = top * dataWidth + left;

  for (int y = 0; y < height; y++) {
    int outputOffset = y * width;
    for (int x = 0; x < width; x++) {
      int grey = yuv[inputOffset + x] & 0xff;
      pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
    }
    inputOffset += dataWidth;
  }

  Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
  return bitmap;
}
 
Example 12
Project: GCSApp   File: PlanarYUVLuminanceSource.java   View Source Code Vote up 6 votes
public Bitmap renderCroppedGreyscaleBitmap() {
    int width = getWidth();
    int height = getHeight();
    int[] pixels = new int[width * height];
    byte[] yuv = yuvData;
    int inputOffset = top * dataWidth + left;

    for (int y = 0; y < height; y++) {
      int outputOffset = y * width;
      for (int x = 0; x < width; x++) {
        int grey = yuv[inputOffset + x] & 0xff;
        pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
      }
      inputOffset += dataWidth;
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
//    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
  }
 
Example 13
Project: Zxing   File: Util.java   View Source Code Vote up 5 votes
/**
 * 根据宽高生成二维码
 *
 * @param width
 * @param height
 * @param source
 * @return
 */
public static Bitmap createQRCode(int width, int height, String source) {
	try {
		if (TextUtils.isEmpty(source)) {
			return null;
		}
		Hashtable<EncodeHintType, String> hints = new Hashtable<>();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		BitMatrix bitMatrix = new QRCodeWriter().encode(source, BarcodeFormat.QR_CODE, width, height, hints);
		int[] pixels = new int[width * height];
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (bitMatrix.get(x, y)) {
					pixels[y * width + x] = 0xff000000;
				} else {
					pixels[y * width + x] = 0xffffffff;
				}
			}
		}
		// sheng chen de er wei ma
		Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		return bitmap;
	} catch (WriterException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 14
Project: keepass2android   File: QRCodeEncoder.java   View Source Code Vote up 5 votes
Bitmap encodeAsBitmap() throws WriterException {
  String contentsToEncode = contents;
  if (contentsToEncode == null) {
    return null;
  }
  Map<EncodeHintType,Object> hints = null;
  String encoding = guessAppropriateEncoding(contentsToEncode);
  if (encoding != null) {
    hints = new EnumMap<EncodeHintType,Object>(EncodeHintType.class);
    hints.put(EncodeHintType.CHARACTER_SET, encoding);
  }
  BitMatrix result;
  try {
    result = new MultiFormatWriter().encode(contentsToEncode, format, dimension, dimension, hints);
  } catch (IllegalArgumentException iae) {
    // Unsupported format
    return null;
  }
  int width = result.getWidth();
  int height = result.getHeight();
  int[] pixels = new int[width * height];
  for (int y = 0; y < height; y++) {
    int offset = y * width;
    for (int x = 0; x < width; x++) {
      pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
    }
  }

  Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
  return bitmap;
}
 
Example 15
Project: Loyalty   File: BarcodeGenerator.java   View Source Code Vote up 5 votes
private Bitmap renderQRCode(String barcode, BarcodeFormat format, int width) throws WriterException {
    float scale = this.context.getResources().getDisplayMetrics().density;

    width *= scale * 0.6;

    Writer barWriter = new MultiFormatWriter();
    Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
    BitMatrix bm = barWriter.encode(barcode, format, width, width);

    for (int j = 0; j < width; j++) {
        int[] row = new int[width];
        for (int i = 0; i < width; i++) {
            row[i] = bm.get(i, j) ? Color.BLACK : Color.TRANSPARENT;
        }
        bitmap.setPixels(row, 0, width, 0, j, width, 1);
    }

    CropUtility cropUtility = new CropUtility();
    Bitmap cropped = cropUtility.rectangularCrop(bitmap, Color.TRANSPARENT, 0);

    FactoryManager manager = new FactoryManager();
    ShapeFactory shapeFactory = manager.getRecommendedShapeFactory();
    return shapeFactory.createShape(new RectangleShape(this.context), cropped, Constants.LOGO_BACKGROUND_COLOUR, padding);
}
 
Example 16
Project: react-native-sunmi-inner-printer   File: BytesUtil.java   View Source Code Vote up 4 votes
protected static Bitmap getBitmapFromData(int[] pixels, int width, int height){
	Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
	bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
	return bitmap;		
}
 
Example 17
Project: XFrame   File: XBitmapUtils.java   View Source Code Vote up 4 votes
/**
 * 更改图片色系,变亮或变暗
 *
 * @param delta 图片的亮暗程度值,越小图片会越亮,取值范围(0,24)
 * @return
 */
public static Bitmap adjustTone(Bitmap src, int delta) {
    if (delta >= 24 || delta <= 0) {
        return null;
    }
    // 设置高斯矩阵
    int[] gauss = new int[]{1, 2, 1, 2, 4, 2, 1, 2, 1};
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Config.RGB_565);

    int pixR = 0;
    int pixG = 0;
    int pixB = 0;
    int pixColor = 0;
    int newR = 0;
    int newG = 0;
    int newB = 0;
    int idx = 0;
    int[] pixels = new int[width * height];

    src.getPixels(pixels, 0, width, 0, 0, width, height);
    for (int i = 1, length = height - 1; i < length; i++) {
        for (int k = 1, len = width - 1; k < len; k++) {
            idx = 0;
            for (int m = -1; m <= 1; m++) {
                for (int n = -1; n <= 1; n++) {
                    pixColor = pixels[(i + m) * width + k + n];
                    pixR = Color.red(pixColor);
                    pixG = Color.green(pixColor);
                    pixB = Color.blue(pixColor);

                    newR += (pixR * gauss[idx]);
                    newG += (pixG * gauss[idx]);
                    newB += (pixB * gauss[idx]);
                    idx++;
                }
            }
            newR /= delta;
            newG /= delta;
            newB /= delta;
            newR = Math.min(255, Math.max(0, newR));
            newG = Math.min(255, Math.max(0, newG));
            newB = Math.min(255, Math.max(0, newB));
            pixels[i * width + k] = Color.argb(255, newR, newG, newB);
            newR = 0;
            newG = 0;
            newB = 0;
        }
    }
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example 18
Project: XFrame   File: XBitmapUtils.java   View Source Code Vote up 4 votes
/**
 * 底片效果处理
 *
 * @param bitmap 原图
 * @return 底片效果处理后的图片
 */
public static Bitmap film(Bitmap bitmap) {
    // RGBA的最大值
    final int MAX_VALUE = 255;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap newBitmap = Bitmap.createBitmap(width, height,
            Config.RGB_565);

    int pixR = 0;
    int pixG = 0;
    int pixB = 0;

    int pixColor = 0;

    int newR = 0;
    int newG = 0;
    int newB = 0;

    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    int pos = 0;
    for (int i = 1, length = height - 1; i < length; i++) {
        for (int k = 1, len = width - 1; k < len; k++) {
            pos = i * width + k;
            pixColor = pixels[pos];

            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);

            newR = MAX_VALUE - pixR;
            newG = MAX_VALUE - pixG;
            newB = MAX_VALUE - pixB;

            newR = Math.min(MAX_VALUE, Math.max(0, newR));
            newG = Math.min(MAX_VALUE, Math.max(0, newG));
            newB = Math.min(MAX_VALUE, Math.max(0, newB));

            pixels[pos] = Color.argb(MAX_VALUE, newR, newG, newB);
        }
    }

    newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBitmap;
}
 
Example 19
Project: ZXingDemo   File: EncodingHandler.java   View Source Code Vote up 4 votes
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);

		//========================================================================
//		//图像数据转换,使用了矩阵转换
//		BitMatrix bitMatrix = new QRCodeWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);
//		int[] pixels = new int[widthAndHeight * widthAndHeight];
//		//下面这里按照二维码的算法,逐个生成二维码的图片,
//		//两个for循环是图片横列扫描的结果
//		for (int y = 0; y < widthAndHeight; y++)
//		{
//			for (int x = 0; x < widthAndHeight; x++)
//			{
//				if (bitMatrix.get(x, y))
//				{
//					pixels[y * widthAndHeight + x] = 0xff000000;
//				}
//				else
//				{
//					pixels[y * widthAndHeight + x] = 0xffffffff;
//				}
//			}
//		}
		//=====================================================================

		int width = matrix.getWidth();
		int height = matrix.getHeight();
		int[] pixels = new int[width * height];
		
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (matrix.get(x, y)) {
					pixels[y * width + x] = BLACK;
				}
			}
		}
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		return bitmap;
	}
 
Example 20
Project: KTools   File: QRCodeUtils.java   View Source Code Vote up 4 votes
/**
 * @param content 二维码内容
 * @param width 宽度px
 * @param height 高度px
 * @param characterSet 字符编码, 支持格式:{@link CharacterSetECI },传null时,zxing源码默认使用 "ISO-8859-1"
 * @param errorCorrection 容错级别,(支持级别:{@link ErrorCorrectionLevel })。传null时,zxing源码默认使用 "L"
 * @param margin 空白边距 (可修改,要求:整型且>=0), 传null时,zxing源码默认使用"4"
 * @param colorBlack 黑色色块的自定义颜色值
 * @param colorWhite 白色色块的自定义颜色值
 * @return 二维码Bitmap
 */
public static Bitmap createQRCodeBitmap(@NonNull String content,
                                        @IntRange(from = 0) int width,
                                        @IntRange(from = 0) int height,
                                        @Nullable String characterSet,
                                        @Nullable String errorCorrection,
                                        @Nullable String margin,
                                        @ColorInt int colorBlack,
                                        @ColorInt int colorWhite
                                        ){
    Hashtable<EncodeHintType,String> hints = new Hashtable<>();

    if (!TextUtils.isEmpty(characterSet)){
        hints.put(EncodeHintType.CHARACTER_SET,characterSet);
    }

    if (!TextUtils.isEmpty(errorCorrection)){
        hints.put(EncodeHintType.ERROR_CORRECTION,errorCorrection);
    }

    if (!TextUtils.isEmpty(margin)){
        hints.put(EncodeHintType.MARGIN,margin);
    }

    try {
        BitMatrix bitMatrix = new QRCodeWriter()
                .encode(content, BarcodeFormat.QR_CODE,width,height,hints);

        /** 3.创建像素数组,并根据BitMatrix(位矩阵)对象为数组元素赋颜色值 */
        int[] pixels = new int[width * height];
        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                if(bitMatrix.get(x, y)){
                    pixels[y * width + x] = colorBlack; // 黑色色块像素设置
                } else {
                    pixels[y * width + x] = colorWhite; // 白色色块像素设置
                }
            }
        }


        Bitmap bitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels,0,width,0,0,width,height);
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }

    return null;
}