Example #1
0
 public void setBitmap() {
   mBitmapWidth = mBitmap.getWidth();
   mBitmapHeight = mBitmap.getHeight();
   mRadius = (int) (mBitmapWidth / 2);
   mBitmapNewHeight =
       (int) Math.round((double) mRadius * Math.sin(Math.PI / (double) mNumberOfMirrors));
   if (mBitmapNewHeight > mBitmapHeight) {
     mBitmapNewHeight = mBitmapHeight / 2;
     mRadius =
         (int)
             Math.round((double) mBitmapNewHeight / Math.sin(Math.PI / (double) mNumberOfMirrors));
   }
   mScale = (float) mBitmapViewWidth / mRadius;
   // Log.i(TAG,String.format("%d", mBitmapViewWidth));
   mBitmapViewHeight = Math.round((float) mBitmapNewHeight * mScale);
   mScaledHeight = (int) (mScale * mBitmapHeight);
   mScaledWidth = (int) (mScale * mBitmapWidth);
   // Log.i(TAG,String.format("%d %d", mRadius, mBitmapNewHeight));
   mAlphaMark = false;
   mViewBitmap = Bitmap.createBitmap(mBitmapViewWidth, mBitmapViewHeight, Bitmap.Config.ARGB_8888);
   mX = mScaledWidth - mBitmapViewWidth;
   mY = mScaledHeight - mBitmapViewHeight;
   mCurX = (int) (Math.random() * mX / mScale);
   mCurY = (int) (Math.random() * mY / mScale);
   mAlpha = mBitmap.hasAlpha();
   // Toast.makeText(mContext, Boolean.toString(mAlpha),
   // Toast.LENGTH_SHORT)
   // .show();
 }
Example #2
0
 public static byte[] bitmapToByteArray(Bitmap bitmap, int compressQuality) {
   ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
   if (bitmap.hasAlpha()) {
     bitmap.compress(Bitmap.CompressFormat.PNG, compressQuality, localByteArrayOutputStream);
   } else {
     bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, localByteArrayOutputStream);
   }
   byte[] result = localByteArrayOutputStream.toByteArray();
   IOUtils.closeQuietly(localByteArrayOutputStream);
   return result;
 }
  @Test
  public void testFitCenterSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlpha() {
    Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

    toTransform.setHasAlpha(false);

    Bitmap result =
        TransformationUtils.fitCenter(
            bitmapPool, toTransform, toTransform.getWidth() / 2, toTransform.getHeight() / 2);

    assertFalse(result.hasAlpha());
  }
Example #4
0
 public int getOpacity()
 {
   if (this.s.c != 119);
   Bitmap localBitmap;
   do
   {
     return -3;
     localBitmap = this.t;
   }
   while ((localBitmap == null) || (localBitmap.hasAlpha()) || (this.s.d.getAlpha() < 255));
   return -1;
 }
 public int getOpacity() {
   if (mGravity != 119) {}
   Bitmap localBitmap;
   do {
     return -3;
     localBitmap = mBitmap;
   } while ((localBitmap == null)
       || (localBitmap.hasAlpha())
       || (mPaint.getAlpha() < 255)
       || (isGreaterThanZero(mCornerRadius)));
   return -1;
 }
  @Test
  public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() {
    Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

    toTransform.setHasAlpha(true);

    Bitmap result =
        TransformationUtils.centerCrop(
            bitmapPool, toTransform, toTransform.getWidth() / 2, toTransform.getHeight() / 2);

    assertTrue(result.hasAlpha());
  }
 @Override
 public int getOpacity() {
   if (mGravity != Gravity.FILL || mIsCircular) {
     return PixelFormat.TRANSLUCENT;
   }
   Bitmap bm = mBitmap;
   return (bm == null
           || bm.hasAlpha()
           || mPaint.getAlpha() < 255
           || isGreaterThanZero(mCornerRadius))
       ? PixelFormat.TRANSLUCENT
       : PixelFormat.OPAQUE;
 }
Example #8
0
  /** @return if map could be replaced */
  public void generateNewBitmapNative(
      RenderingContext rc,
      NativeOsmandLibrary library,
      NativeSearchResult searchResultHandler,
      Bitmap bmp,
      RenderingRuleSearchRequest render,
      final List<IMapDownloaderCallback> notifyList) {
    long now = System.currentTimeMillis();
    if (rc.width > 0 && rc.height > 0 && searchResultHandler != null) {
      // init rendering context
      rc.tileDivisor = (int) (1 << (31 - rc.zoom));
      rc.cosRotateTileSize = FloatMath.cos((float) Math.toRadians(rc.rotate)) * TILE_SIZE;
      rc.sinRotateTileSize = FloatMath.sin((float) Math.toRadians(rc.rotate)) * TILE_SIZE;
      try {
        if (Looper.getMainLooper() != null && library.useDirectRendering()) {
          final Handler h = new Handler(Looper.getMainLooper());
          notifyListenersWithDelay(rc, notifyList, h);
        }

        // Native library will decide on it's own best way of rendering
        // If res.bitmapBuffer is null, it indicates that rendering was done directly to
        // memory of passed bitmap, but this is supported only on Android >= 2.2
        final NativeLibrary.RenderingGenerationResult res =
            library.generateRendering(rc, searchResultHandler, bmp, bmp.hasAlpha(), render);
        rc.ended = true;
        notifyListeners(notifyList);
        long time = System.currentTimeMillis() - now;
        rc.renderingDebugInfo =
            String.format(
                "Rendering: %s ms  (%s text)\n"
                    + "(%s points, %s points inside, %s of %s objects visible)\n", //$NON-NLS-1$
                time,
                rc.textRenderingTime,
                rc.pointCount,
                rc.pointInsideCount,
                rc.visible,
                rc.allObjects);

        // See upper note
        if (res.bitmapBuffer != null) {
          bmp.copyPixelsFromBuffer(res.bitmapBuffer);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  @Test
  public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlphaAndOutBitmapIsReused() {
    Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

    Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
    reset(bitmapPool);
    when(bitmapPool.get(eq(50), eq(50), eq(Bitmap.Config.ARGB_8888))).thenReturn(toReuse);

    toReuse.setHasAlpha(false);
    toTransform.setHasAlpha(true);

    Bitmap result =
        TransformationUtils.centerCrop(
            bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight());

    assertEquals(toReuse, result);
    assertTrue(result.hasAlpha());
  }
  private boolean decodeBitmapStreamSafe(Uri target) {
    String path = uriToFilePath(target, this);
    int degree = 0;
    if (path != null) {
      degree = readBitmapDegree(path);
    }

    InputStream is = null;

    boolean bool = true;
    int scale = 1;
    Options opt = new Options();
    while (bool) {
      try {
        is = mContentResolver.openInputStream(target);
        opt.inSampleSize = scale;
        mBitmap = null;
        mBitmap = BitmapFactory.decodeStream(is, null, opt);
        mBitmap = postRotateToBitmap(degree, mBitmap);
        mHasApha = mBitmap.hasAlpha();
        bool = false;

        return true;
      } catch (OutOfMemoryError e) {
        // 如果解碼大圖片,出现爆内存,则每次缩放一半
        OutOfMemoryHandler.handle();
        scale *= 2;
        if (scale > (1 << 10)) {
          // 防止异常死循环
          return false;
        }
      } catch (Throwable e) {
        // TODO: handle Throwable
        bool = false;

        return false;
      }
    }
    return false;
  }
Example #11
0
  /**
   * 解码图片
   *
   * @param image 图片
   * @return 数据流
   */
  @SuppressWarnings("unused")
  public static byte[] decodeBitmap(Bitmap image) {
    if (image == null) return null;
    final int width = image.getWidth();
    final int height = image.getHeight();
    if (width <= 0 || height <= 0) return null;

    if (width > 2040) {
      // 8位9针,宽度限制2040像素(但一般纸张都没法打印那么宽,但并不影响打印)
      final float scale = 2040 / (float) width;
      Matrix matrix = new Matrix();
      matrix.postScale(scale, scale);
      Bitmap resizeImage;
      try {
        resizeImage = Bitmap.createBitmap(image, 0, 0, width, height, matrix, true);
      } catch (OutOfMemoryError e) {
        return null;
      }
      byte[] data = decodeBitmap(resizeImage);
      resizeImage.recycle();
      return data;
    }

    // 宽命令
    String widthHexString = Integer.toHexString(width % 8 == 0 ? width / 8 : (width / 8 + 1));
    if (widthHexString.length() > 2) {
      // 超过2040像素才会到达这里
      return null;
    } else if (widthHexString.length() == 1) {
      widthHexString = "0" + widthHexString;
    }
    widthHexString += "00";

    // 每行字节数(除以8,不足补0)
    String zeroStr = "";
    int zeroCount = width % 8;
    if (zeroCount > 0) {
      for (int i = 0; i < (8 - zeroCount); i++) {
        zeroStr += "0";
      }
    }
    ArrayList<String> commandList = new ArrayList<>();
    // 高度每255像素进行一次分割
    int time = height % 255 == 0 ? height / 255 : (height / 255 + 1); // 循环打印次数
    for (int t = 0; t < time; t++) {
      int partHeight = t == time - 1 ? height % 255 : 255; // 分段高度

      // 高命令
      String heightHexString = Integer.toHexString(partHeight);
      if (heightHexString.length() > 2) {
        // 超过255像素才会到达这里
        return null;
      } else if (heightHexString.length() == 1) {
        heightHexString = "0" + heightHexString;
      }
      heightHexString += "00";

      // 宽高指令
      String commandHexString = "1D763000";
      commandList.add(commandHexString + widthHexString + heightHexString);

      ArrayList<String> list = new ArrayList<>(); // binaryString list
      StringBuilder sb = new StringBuilder();
      // 像素二值化,非黑即白
      for (int i = 0; i < partHeight; i++) {
        sb.delete(0, sb.length());
        for (int j = 0; j < width; j++) {
          // 实际在图片中的高度
          int startHeight = t * 255 + i;
          // 得到当前像素的值
          int color = image.getPixel(j, startHeight);
          int red, green, blue;
          if (image.hasAlpha()) {
            // 得到alpha通道的值
            int alpha = Color.alpha(color);
            // 得到图像的像素RGB的值
            red = Color.red(color);
            green = Color.green(color);
            blue = Color.blue(color);
            final float offset = alpha / 255.0f;
            // 根据透明度将白色与原色叠加
            red = 0xFF + (int) Math.ceil((red - 0xFF) * offset);
            green = 0xFF + (int) Math.ceil((green - 0xFF) * offset);
            blue = 0xFF + (int) Math.ceil((blue - 0xFF) * offset);
          } else {
            // 得到图像的像素RGB的值
            red = Color.red(color);
            green = Color.green(color);
            blue = Color.blue(color);
          }
          // 接近白色改为白色。其余黑色
          if (red > 160 && green > 160 && blue > 160) sb.append("0");
          else sb.append("1");
        }
        // 每一行结束时,补充剩余的0
        if (zeroCount > 0) {
          sb.append(zeroStr);
        }
        list.add(sb.toString());
      }
      // binaryStr每8位调用一次转换方法,再拼合
      ArrayList<String> bmpHexList = new ArrayList<>();
      for (String binaryStr : list) {
        sb.delete(0, sb.length());
        for (int i = 0; i < binaryStr.length(); i += 8) {
          String str = binaryStr.substring(i, i + 8);
          // 2进制转成16进制
          String hexString = binaryStrToHexString(str);
          sb.append(hexString);
        }
        bmpHexList.add(sb.toString());
      }

      // 数据指令
      commandList.addAll(bmpHexList);
    }
    return hexListToByte(commandList);
  }
  public Boolean LoadImage(int resourceId) {
    Bitmap partyOBits = BitmapFactory.decodeResource(NovaCraft.instance.getResources(), resourceId);
    if (partyOBits == null) {
      Log.e(NovaCraft.TAG, "Failed to load image:" + resourceId);
      return false;
    }

    resourceID = resourceId;

    int bpp = partyOBits.hasAlpha() ? 4 : 3;
    ByteBuffer bb = ByteBuffer.allocateDirect(partyOBits.getHeight() * partyOBits.getWidth() * bpp);
    bb.order(ByteOrder.nativeOrder());
    partyOBits.copyPixelsToBuffer(bb);
    bb.position(0);

    NovaRenderer.gl.glPixelStorei(NovaRenderer.gl.GL_UNPACK_ALIGNMENT, 1);

    NovaRenderer.gl.glGetError();
    if (texID != 0) {
      int[] texIDA = new int[1];
      texIDA[0] = texID;
      NovaRenderer.gl.glDeleteTextures(1, texIDA, 0);
      texID = 0;
    }

    int[] texIDA = new int[1];
    NovaRenderer.gl.glGenTextures(1, texIDA, 0);
    texID = texIDA[0];
    NovaRenderer.gl.glBindTexture(NovaRenderer.gl.GL_TEXTURE_2D, texID);

    NovaRenderer.gl.glTexImage2D(
        NovaRenderer.gl.GL_TEXTURE_2D,
        0,
        partyOBits.hasAlpha() ? NovaRenderer.gl.GL_RGBA : NovaRenderer.gl.GL_RGB,
        partyOBits.getWidth(),
        partyOBits.getHeight(),
        0,
        partyOBits.hasAlpha() ? NovaRenderer.gl.GL_RGBA : NovaRenderer.gl.GL_RGB,
        partyOBits.hasAlpha()
            ? NovaRenderer.gl.GL_UNSIGNED_BYTE
            : NovaRenderer.gl.GL_UNSIGNED_SHORT_5_6_5,
        bb);
    if (NovaRenderer.gl.glGetError() > 0) {
      Log.e(NovaCraft.TAG, "Failed to load texture:" + resourceId);
      return false;
    } else
      Log.v(
          NovaCraft.TAG,
          "Texture loaded:"
              + resourceId
              + " with GL ID:"
              + texID
              + " width: "
              + partyOBits.getWidth()
              + " height: "
              + partyOBits.getHeight());

    NovaRenderer.gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    NovaRenderer.gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    width = partyOBits.getWidth();
    height = partyOBits.getHeight();

    texCoordBuffers = null;

    ByteBuffer tbb = ByteBuffer.allocateDirect(texCoordArray_Full.length * 4);
    tbb.order(ByteOrder.nativeOrder());
    texCoordBuffer = tbb.asFloatBuffer();
    texCoordBuffer.put(texCoordArray_Full);
    texCoordBuffer.position(0);

    return true;
  }