private static Bitmap getWhiteSheep(File inputDir) {
    File alreadyThere = new File(inputDir, "mob/sheep_0.png");
    if (alreadyThere.exists()) {
      return BitmapFactory.decodeFile(alreadyThere.getAbsolutePath());
    }
    File topHalf = new File(inputDir, "mob/sheep.png");
    if (!topHalf.exists()) {
      topHalf = new File(inputDir, "entity/sheep/sheep.png");
    }

    // We have to build this one by hand
    File bottomHalf = new File(inputDir, "mob/sheep_fur.png");
    if (!bottomHalf.exists()) {
      bottomHalf = new File(inputDir, "entity/sheep/sheep_fur.png");
    }
    if (!topHalf.exists() || !bottomHalf.exists()) {
      return null;
    }
    Bitmap topHalfBitmap = BitmapFactory.decodeFile(topHalf.getAbsolutePath());
    Bitmap bottomHalfBitmap = BitmapFactory.decodeFile(bottomHalf.getAbsolutePath());
    Bitmap finalBitmap =
        Bitmap.createBitmap(
            topHalfBitmap.getWidth(), topHalfBitmap.getHeight() * 2, Bitmap.Config.ARGB_8888);
    int[] pixels = new int[topHalfBitmap.getWidth() * topHalfBitmap.getHeight()];
    topHalfBitmap.getPixels(
        pixels,
        0,
        topHalfBitmap.getWidth(),
        0,
        0,
        topHalfBitmap.getWidth(),
        topHalfBitmap.getHeight());
    finalBitmap.setPixels(
        pixels,
        0,
        topHalfBitmap.getWidth(),
        0,
        0,
        topHalfBitmap.getWidth(),
        topHalfBitmap.getHeight());
    bottomHalfBitmap.getPixels(
        pixels,
        0,
        topHalfBitmap.getWidth(),
        0,
        0,
        topHalfBitmap.getWidth(),
        topHalfBitmap.getHeight());
    finalBitmap.setPixels(
        pixels,
        0,
        topHalfBitmap.getWidth(),
        0,
        topHalfBitmap.getHeight(),
        topHalfBitmap.getWidth(),
        topHalfBitmap.getHeight());
    return finalBitmap;
  }
示例#2
0
  public static Bitmap encodeAsBitmap(
      String contents, BarcodeFormat format, int desiredWidth, int desiredHeight)
      throws WriterException {
    final int WHITE = 0xFFFFFFFF; // 可以指定其他颜色,让二维码变成彩色效果
    final int BLACK = 0xFF000000;

    Hashtable<EncodeHintType, String> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
      hints = new Hashtable<EncodeHintType, String>(2);
      hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, desiredWidth, desiredHeight, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    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;
  }
  public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
      hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
      hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    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;
  }
示例#4
0
 public static Bitmap cretaeBitmap(Context c, String str, Bitmap mBitmap) throws WriterException {
   // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
   int qr_width = ScreenUtils.getScreenWidth(c) * 2 / 3;
   int qr_height = qr_width;
   BitMatrix matrix =
       new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, qr_width, qr_height);
   //        BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300,
   // 300);//如果要指定二维码的边框以及容错率,最好给encode方法增加一个参数:hints 一个Hashmap
   int width = matrix.getWidth();
   int height = matrix.getHeight();
   // 二维矩阵转为一维像素数组,也就是一直横着排了
   int halfW = width / 2;
   int halfH = height / 2;
   int[] pixels = new int[width * height];
   for (int y = 0; y < height; y++) {
     for (int x = 0; x < width; x++) {
       if (x > halfW - Const.IMAGE_HALFWIDTH
           && x < halfW + Const.IMAGE_HALFWIDTH
           && y > halfH - Const.IMAGE_HALFWIDTH
           && y < halfH + Const.IMAGE_HALFWIDTH) {
         pixels[y * width + x] =
             mBitmap.getPixel(
                 x - halfW + Const.IMAGE_HALFWIDTH, y - halfH + Const.IMAGE_HALFWIDTH);
       } else {
         // 此处可以修改二维码的颜色,可以分别制定二维码和背景的颜色;
         pixels[y * width + x] = matrix.get(x, y) ? 0xff000000 : 0xfffffff;
       }
     }
   }
   Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
   // 通过像素数组生成bitmap
   bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
   return bitmap;
 }
  private Bitmap createBitmap(String content, final int size) {
    final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    hints.put(EncodeHintType.MARGIN, 0);
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    BitMatrix result;
    try {
      result = sQRCodeWriter.encode(content, BarcodeFormat.QR_CODE, size, size, hints);
    } catch (WriterException ex) {
      mLogger.warn("qr encoder failed: " + ex.toString());
      return null;
    }

    final int width = result.getWidth();
    final int height = result.getHeight();
    final int[] pixels = new int[width * height];

    for (int y = 0; y < height; y++) {
      final int offset = y * width;
      for (int x = 0; x < width; x++) {
        pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
      }
    }

    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

    return bitmap;
  }
示例#6
0
  private static MatrixToImageResult matrixToImage(BitMatrix matrix, BarcodeFormat type) {
    if (matrix != null) {
      try {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

        int onColor = 0xFF000000;
        int offColor = 0xFFFFFFFF;
        int[] pixels = new int[width * height];
        int index = 0;
        for (int y = 0; y < height; y++) {
          for (int x = 0; x < width; x++) {
            pixels[index++] = matrix.get(x, y) ? onColor : offColor;
          }
        }
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

        File file =
            new File(
                Extension.mainContext.getApplicationInfo().dataDir,
                "code_" + type.toString() + ".jpg");
        FileOutputStream out = new FileOutputStream(file);
        if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
          out.flush();
          out.close();
          return new MatrixToImageResult(true, file.getAbsolutePath());
        } else return new MatrixToImageResult(false, null);
      } catch (Exception e) {
        e.printStackTrace();
        return new MatrixToImageResult(false, null);
      }
    }
    return new MatrixToImageResult(false, null);
  }
示例#7
0
  public 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.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;
  }
示例#8
0
 private static Bitmap toBitmap(LuminanceSource source, int[] pixels) {
   int width = source.getWidth();
   int height = source.getHeight();
   Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
   bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
   return bitmap;
 }
示例#9
0
  public static Bitmap bitmap(@Nonnull final String content, final int size) {
    try {
      final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
      hints.put(EncodeHintType.MARGIN, 0);
      hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
      final BitMatrix result =
          QR_CODE_WRITER.encode(content, BarcodeFormat.QR_CODE, size, size, hints);

      final int width = result.getWidth();
      final int height = result.getHeight();
      final int[] pixels = new int[width * height];

      for (int y = 0; y < height; y++) {
        final int offset = y * width;
        for (int x = 0; x < width; x++) {
          pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
        }
      }

      final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
      return bitmap;
    } catch (final WriterException x) {
      log.info("problem creating qr code", x);
      return null;
    }
  }
 // 要转换的地址或字符串,可以是中文
 public void createQRImage(String url) {
   try {
     // 判断URL合法性
     if (url == null || "".equals(url) || url.length() < 1) {
       return;
     }
     Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
     hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
     // 图像数据转换,使用了矩阵转换
     BitMatrix bitMatrix =
         new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
     int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
     // 下面这里按照二维码的算法,逐个生成二维码的图片,
     // 两个for循环是图片横列扫描的结果
     for (int y = 0; y < QR_HEIGHT; y++) {
       for (int x = 0; x < QR_WIDTH; x++) {
         if (bitMatrix.get(x, y)) {
           pixels[y * QR_WIDTH + x] = 0xff000000;
         } else {
           pixels[y * QR_WIDTH + x] = 0xffffffff;
         }
       }
     }
     // 生成二维码图片的格式,使用ARGB_8888
     Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
     bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
     // 显示到一个ImageView上面
     sweepIV.setImageBitmap(bitmap);
   } catch (WriterException e) {
     e.printStackTrace();
   }
 }
示例#11
0
  @Override
  public void run(Canvas canvas, Bitmap bitmap) {
    setChanged();
    notifyStatus(NOTIFY_STATES.COMMAND_STARTED);
    if (mClickedPixel == null) {
      setChanged();
      notifyStatus(NOTIFY_STATES.COMMAND_FAILED);
      return;
    }
    int colorToReplace = bitmap.getPixel(mClickedPixel.x, mClickedPixel.y);
    int pixels[] = new int[bitmap.getWidth() * bitmap.getHeight()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

    QueueLinearFloodFiller.floodFill(
        pixels,
        bitmap.getWidth(),
        bitmap.getHeight(),
        mClickedPixel,
        colorToReplace,
        mPaint.getColor(),
        SELECTION_THRESHOLD);

    bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

    setChanged();
    notifyStatus(NOTIFY_STATES.COMMAND_DONE);
  }
示例#12
0
  public static Bitmap applySnowEffect(Bitmap source) {
    // get image size
    int width = source.getWidth();
    int height = source.getHeight();
    int[] pixels = new int[width * height];
    // get pixel array from source
    source.getPixels(pixels, 0, width, 0, 0, width, height);
    // random object
    Random random = new Random();

    int R, G, B, index = 0, thresHold = 50;
    // iteration through pixels
    for (int y = 0; y < height; ++y) {
      for (int x = 0; x < width; ++x) {
        // get current index in 2D-matrix
        index = y * width + x;
        // get color
        R = Color.red(pixels[index]);
        G = Color.green(pixels[index]);
        B = Color.blue(pixels[index]);
        // generate threshold
        thresHold = random.nextInt(COLOR_MAX);
        if (R > thresHold && G > thresHold && B > thresHold) {
          pixels[index] = Color.rgb(255, 255, 255);
        }
      }
    }
    // output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
    return bmOut;
  }
示例#13
0
    private void createBitmap() {
      if (null == bitmap) return;

      int selectorHalfWidth = 1;
      int w = getWidth();
      int selectedValue = (int) (colorHsv[2] * (w - selectorHalfWidth * 2));

      float value = 0;
      float valueStep = 1f / w;
      float[] hsvTmp = new float[] {colorHsv[0], colorHsv[1], 1f};
      for (int x = 0; x < w; x++) {
        value += valueStep;
        if (x >= selectedValue - selectorHalfWidth && x <= selectedValue + selectorHalfWidth) {
          int intVal = 0xFF - (int) (value * 0xFF);
          int selectorColor = intVal * 0x010101 + 0xFF000000;
          pixels[x] = selectorColor;
        } else {
          hsvTmp[2] = value;
          pixels[x] = Color.HSVToColor(hsvTmp);
        }
      }

      bitmap.setPixels(pixels, 0, w, 0, 0, w, 1);
      invalidate();
    }
示例#14
0
  @Override
  public void run(Canvas canvas, Bitmap bitmap) {

    notifyStatus(NOTIFY_STATES.COMMAND_STARTED);
    if (mClickedPixel == null) {
      setChanged();
      notifyStatus(NOTIFY_STATES.COMMAND_FAILED);
      return;
    }

    if (PaintroidApplication.savedPictureUri == null
        && PaintroidApplication.commandManager.getNumberOfCommands()
            == EMPTY_COMMAND_LIST_LENGTH + 1) {
      canvas.drawColor(mPaint.getColor());
      Log.w(PaintroidApplication.TAG, "Fill Command color: " + mPaint.getColor());
    } else {
      int colorToReplace = bitmap.getPixel(mClickedPixel.x, mClickedPixel.y);
      int pixels[] = new int[bitmap.getWidth() * bitmap.getHeight()];
      bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

      QueueLinearFloodFiller.floodFill(
          pixels,
          bitmap.getWidth(),
          bitmap.getHeight(),
          mClickedPixel,
          colorToReplace,
          mPaint.getColor(),
          SELECTION_THRESHOLD);

      bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    }

    notifyStatus(NOTIFY_STATES.COMMAND_DONE);
  }
  /**
   * Get the white icon corresponding to a poiType.
   *
   * @param poiType the PoiType or null for notes.
   * @return The white icon.
   */
  public Drawable getIconWhite(PoiType poiType) {
    Bitmap myBitmap =
        BitmapFactory.decodeResource(
            context.getResources(),
            poiType == null ? R.drawable.open_book : getIconDrawableId(poiType));
    myBitmap = myBitmap.copy(myBitmap.getConfig(), true);

    int[] allpixels = new int[myBitmap.getHeight() * myBitmap.getWidth()];

    myBitmap.getPixels(
        allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

    for (int i = 0; i < myBitmap.getHeight() * myBitmap.getWidth(); i++) {
      if (allpixels[i] != 0) {
        int A = Color.alpha(allpixels[i]);
        // inverting byte for each R/G/B channel
        int R = 255 - Color.red(allpixels[i]);
        int G = 255 - Color.green(allpixels[i]);
        int B = 255 - Color.blue(allpixels[i]);
        // set newly-inverted pixel to output image
        allpixels[i] = Color.argb(A, R, G, B);
      }
    }

    myBitmap.setPixels(
        allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

    return new BitmapDrawable(context.getResources(), myBitmap);
  }
示例#16
0
  // 版画效果函数
  public static Bitmap changeToBlock(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    int dst[] = new int[width * height];
    bitmap.getPixels(dst, 0, width, 0, 0, width, height);

    int iPixel = 0;
    int i, j, color, pos;
    for (j = 0; j < height; j++) {
      for (i = 0; i < width; i++) {
        pos = j * width + i;
        color = dst[pos];
        int avg = (Color.red(color) + Color.green(color) + Color.blue(color)) / 3;
        if (avg >= 100) iPixel = 255;
        else iPixel = 0;

        dst[pos] = Color.rgb(iPixel, iPixel, iPixel);
      }
    }

    Bitmap bmpReturn = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    bmpReturn.setPixels(dst, 0, width, 0, 0, width, height);
    return bmpReturn;
  }
示例#17
0
  public static Bitmap applySaturationFilter(Bitmap source, int level) {
    // get image size
    int width = source.getWidth();
    int height = source.getHeight();
    int[] pixels = new int[width * height];
    float[] HSV = new float[3];
    // get pixel array from source
    source.getPixels(pixels, 0, width, 0, 0, width, height);

    int index = 0;
    // iteration through pixels
    for (int y = 0; y < height; ++y) {
      for (int x = 0; x < width; ++x) {
        // get current index in 2D-matrix
        index = y * width + x;
        // convert to HSV
        Color.colorToHSV(pixels[index], HSV);
        // increase Saturation level
        HSV[1] *= level;
        HSV[1] = (float) Math.max(0.0, Math.min(HSV[1], 1.0));
        // take color back
        pixels[index] |= Color.HSVToColor(HSV);
      }
    }
    // output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
    return bmOut;
  }
示例#18
0
  public static Bitmap getMinimalQRCodeBitmap(String url) {
    Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    hints.put(EncodeHintType.MARGIN, 5);

    try {
      final BitMatrix result = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, 0, 0, hints);

      final int width = result.getWidth();
      final int height = result.getHeight();
      final int[] pixels = new int[width * height];

      for (int y = 0; y < height; y++) {
        final int offset = y * width;
        for (int x = 0; x < width; x++) {
          pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
        }
      }

      final Bitmap smallBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      smallBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
      return smallBitmap;
    } catch (final WriterException x) {
      x.printStackTrace();
      return null;
    }
  }
 protected Bitmap createQrCodeBitmap(String input, int size) {
   Log.d(Config.LOGTAG, "qr code requested size: " + size);
   try {
     final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
     final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
     hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
     final BitMatrix result =
         QR_CODE_WRITER.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
     final int width = result.getWidth();
     final int height = result.getHeight();
     final int[] pixels = new int[width * height];
     for (int y = 0; y < height; y++) {
       final int offset = y * width;
       for (int x = 0; x < width; x++) {
         pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
       }
     }
     final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
     Log.d(Config.LOGTAG, "output size: " + width + "x" + height);
     bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
     return bitmap;
   } catch (final WriterException e) {
     return null;
   }
 }
示例#20
0
 private void updateFinished() {
   which = (which + 1) % 2;
   Bitmap bmp = buffers[which];
   bmp.setPixels(bitmapBuffer, 0, 256, 0, 0, 256, 256);
   setImageBitmap(bmp);
   // invalidate();
   postInvalidate();
 }
示例#21
0
  /**
   * Fast Gaussian blurring algorithm source:
   * https://github.com/patrickfav/BlurTestAndroid/blob/master/BlurBenchmark/src/main/java/at/favre/app/blurbenchmark/blur/algorithms/GaussianFastBlur.java
   */
  @NonNull
  public static Bitmap applyFastGaussianBlurToBitmap(@NonNull Bitmap mutableBitmap, int radius) {
    int w = mutableBitmap.getWidth();
    int h = mutableBitmap.getHeight();
    int[] pixels = new int[w * h];
    mutableBitmap.getPixels(pixels, 0, w, 0, 0, w, h);

    for (int r = radius; r >= 1; r /= 2) {
      for (int i = r; i < h - r; i++) {
        for (int j = r; j < w - r; j++) {
          int tl = pixels[(i - r) * w + j - r];
          int tr = pixels[(i - r) * w + j + r];
          int tc = pixels[(i - r) * w + j];
          int bl = pixels[(i + r) * w + j - r];
          int br = pixels[(i + r) * w + j + r];
          int bc = pixels[(i + r) * w + j];
          int cl = pixels[i * w + j - r];
          int cr = pixels[i * w + j + r];

          pixels[(i * w) + j] =
              0xFF000000
                  | (((tl & 0xFF)
                              + (tr & 0xFF)
                              + (tc & 0xFF)
                              + (bl & 0xFF)
                              + (br & 0xFF)
                              + (bc & 0xFF)
                              + (cl & 0xFF)
                              + (cr & 0xFF))
                          >> 3)
                      & 0xFF
                  | (((tl & 0xFF00)
                              + (tr & 0xFF00)
                              + (tc & 0xFF00)
                              + (bl & 0xFF00)
                              + (br & 0xFF00)
                              + (bc & 0xFF00)
                              + (cl & 0xFF00)
                              + (cr & 0xFF00))
                          >> 3)
                      & 0xFF00
                  | (((tl & 0xFF0000)
                              + (tr & 0xFF0000)
                              + (tc & 0xFF0000)
                              + (bl & 0xFF0000)
                              + (br & 0xFF0000)
                              + (bc & 0xFF0000)
                              + (cl & 0xFF0000)
                              + (cr & 0xFF0000))
                          >> 3)
                      & 0xFF0000;
        }
      }
    }

    mutableBitmap.setPixels(pixels, 0, w, 0, 0, w, h);
    return mutableBitmap;
  }
  /**
   * Execute filter.
   *
   * @return the bitmap
   */
  @Override
  public Bitmap executeFilter() {

    final long time = System.currentTimeMillis();
    if (this.getBitOffset() == 0) {
      this.setBitOffset(32);
    } else if (this.getBitOffset() == 1) {
      this.setBitOffset(64);
    } else if (this.getBitOffset() == 2) {
      this.setBitOffset(128);
    }

    final int width = this.getBitmapIn().getWidth();
    final int height = this.getBitmapIn().getHeight();
    final int[] pixels = new int[width * height];
    int red, green, blue;
    getBitmapIn().getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i = 0; i < pixels.length; i++) {

      red = Color.red(pixels[i]);
      green = Color.green(pixels[i]);
      blue = Color.blue(pixels[i]);

      red =
          ((red + (this.getBitOffset() / 2))
              - ((red + (this.getBitOffset() / 2)) % this.getBitOffset())
              - 1);
      if (red < 0) {
        red = 0;
      }

      green =
          ((green + (this.getBitOffset() / 2))
              - ((green + (this.getBitOffset() / 2)) % this.getBitOffset())
              - 1);
      if (green < 0) {
        green = 0;
      }

      blue =
          ((blue + (this.getBitOffset() / 2))
              - ((blue + (this.getBitOffset() / 2)) % this.getBitOffset())
              - 1);
      if (blue < 0) {
        blue = 0;
      }

      pixels[i] = Color.argb(Color.alpha(pixels[i]), red, green, blue);
    }
    final Bitmap bitmapOut = Bitmap.createBitmap(width, height, this.getBitmapIn().getConfig());
    bitmapOut.setPixels(pixels, 0, width, 0, 0, width, height);
    System.out.println("Finished @ " + (System.currentTimeMillis() - time) + "ms");

    return bitmapOut;
  }
示例#23
0
  public static Bitmap sharpenImageAmeliorate(Bitmap bmp) {
    if (ImageCache.get("sharpenImageAmeliorate") != null) {
      return ImageCache.get("sharpenImageAmeliorate");
    }
    int[] laplacian = new int[] {-1, -1, -1, -1, 9, -1, -1, -1, -1};

    int width = bmp.getWidth();
    int height = bmp.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.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;
    float alpha = 0.3F;
    int[] pixels = new int[width * height];
    bmp.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 + n) * width + k + m];
            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);

            newR = newR + (int) (pixR * laplacian[idx] * alpha);
            newG = newG + (int) (pixG * laplacian[idx] * alpha);
            newB = newB + (int) (pixB * laplacian[idx] * alpha);
            idx++;
          }
        }

        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);
    ImageCache.put("sharpenImageAmeliorate", bitmap);
    return bitmap;
  }
示例#24
0
  public static Bitmap sunshine(Bitmap bmp, int centerX, int centerY) {
    final int width = bmp.getWidth();
    final int height = bmp.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.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 radius = Math.min(centerX, centerY);

    final float strength = 150F;
    int[] pixels = new int[width * height];
    bmp.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 = pixR;
        newG = pixG;
        newB = pixB;

        int distance = (int) (Math.pow((centerY - i), 2) + Math.pow(centerX - k, 2));
        if (distance < radius * radius) {

          int result = (int) (strength * (1.0 - Math.sqrt(distance) / radius));
          newR = pixR + result;
          newG = pixG + result;
          newB = pixB + result;
        }

        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[pos] = Color.argb(255, newR, newG, newB);
      }
    }

    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
  }
示例#25
0
  public Bitmap getResBitmapMutable(Resources res, int bmpResId) {
    Options opts = new Options();
    opts.inDither = false;
    Bitmap b = BitmapFactory.decodeResource(res, bmpResId, opts);

    int w = b.getWidth();
    int h = b.getHeight();
    Bitmap mutable = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    int[] pixels = new int[w * h];
    b.getPixels(pixels, 0, w, 0, 0, w, h);
    mutable.setPixels(pixels, 0, w, 0, 0, w, h);
    return mutable;
  }
示例#26
0
 Bitmap decodeViaLibrary(byte[] encoded) {
   int[] width = new int[] {0};
   int[] height = new int[] {0};
   byte[] decoded = libwebp.WebPDecodeARGB(encoded, encoded.length, width, height);
   if (width[0] == 0 || height[0] == 0 || decoded == null) {
     return null;
   }
   int[] pixels = new int[decoded.length / 4];
   ByteBuffer.wrap(decoded).asIntBuffer().get(pixels);
   Bitmap bm = Bitmap.createBitmap(width[0], height[0], Bitmap.Config.ARGB_8888);
   bm.setPixels(pixels, 0, width[0], 0, 0, width[0], height[0]);
   decoded = null;
   return bm;
 }
示例#27
0
  public Bitmap createBitmap(BitMatrix matrix) {
    int width = matrix.getWidth();
    int height = matrix.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] = matrix.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;
  }
示例#28
0
  @Override
  protected Bitmap processFrame(byte[] data) {
    int[] rgba = mRGBA;

    FindFeatures(getFrameWidth(), getFrameHeight(), data, rgba);

    Bitmap bmp = mBitmap;
    bmp.setPixels(
        rgba,
        0 /* offset */,
        getFrameWidth() /* stride */,
        0,
        0,
        getFrameWidth(),
        getFrameHeight());
    return bmp;
  }
  void fillBitmap() {
    int w = mBitmap.getWidth();
    int h = mBitmap.getHeight();

    for (int x = 0; x < w; x++) {
      float hue = 360 * (x) / (float) w;

      mTmpHSV[0] = hue;
      mTmpHSV[1] = 1;
      mTmpHSV[2] = 1;
      int color = Color.HSVToColor(mTmpHSV);
      mTmpBuff[x] = color;
      mTmpBuff[x + w] = color;
    }

    mBitmap.setPixels(mTmpBuff, 0, w, 0, 0, w, h);
  }
示例#30
0
  public void TakeGLScreenshot(GL10 gl) {
    if (mStrSnapshotName != "") {
      getManagers().getUsageStatisticsManager().TrackEvent("Screenshot", "Count", 0);

      int[] mViewPort = new int[4];
      GL11 gl2 = (GL11) gl;
      gl2.glGetIntegerv(GL11.GL_VIEWPORT, mViewPort, 0);

      int width = mViewPort[2];
      int height = mViewPort[3];

      int size = width * height;
      ByteBuffer buf = ByteBuffer.allocateDirect(size * 4);
      buf.order(ByteOrder.nativeOrder());
      gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, buf);
      int data[] = new int[size];
      buf.asIntBuffer().get(data);
      buf = null;
      Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
      bitmap.setPixels(data, size - width, -width, 0, 0, width, height);
      data = null;

      short sdata[] = new short[size];
      ShortBuffer sbuf = ShortBuffer.wrap(sdata);
      bitmap.copyPixelsToBuffer(sbuf);
      for (int i = 0; i < size; ++i) {
        // BGR-565 to RGB-565
        short v = sdata[i];
        sdata[i] = (short) ((v & 0x1f) << 11 | v & 0x7e0 | (v & 0xf800) >> 11);
      }
      sbuf.rewind();
      bitmap.copyPixelsFromBuffer(sbuf);

      try {
        FileOutputStream fos = new FileOutputStream(mStrSnapshotName);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
      } catch (Exception e) {
        e.printStackTrace();
        return;
      }

      mStrSnapshotName = ""; // reset
    }
  }