Java Code Examples for android.graphics.Bitmap#getHeight()

The following examples show how to use android.graphics.Bitmap#getHeight() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

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

  Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  }

  Canvas canvas = new Canvas(bitmap);
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
  drawRoundRect(canvas, paint, width, height);
  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 2
Source Project: ImageSelector   File: ImageUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static Bitmap zoomBitmap(Bitmap bm, int reqWidth, int reqHeight) {
    // 获得图片的宽高
    int width = bm.getWidth();
    int height = bm.getHeight();
    // 计算缩放比例
    float scaleWidth = ((float) reqWidth) / width;
    float scaleHeight = ((float) reqHeight) / height;
    float scale = Math.min(scaleWidth, scaleHeight);
    // 取得想要缩放的matrix参数
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    // 得到新的图片
    Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
            true);
    return newbm;
}
 
Example 3
private IRBThumbnail createThumbnail(Bitmap thumbnail) throws IOException {
	// Create memory buffer to write data
	ByteArrayOutputStream bout = new ByteArrayOutputStream();
	// Compress the thumbnail
	try {
		thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bout);
	} catch (Exception e) {
		e.printStackTrace();
	}
	byte[] data = bout.toByteArray();
	this.id = ImageResourceID.THUMBNAIL_RESOURCE_PS5;
	// Write thumbnail dimension
	this.width = thumbnail.getWidth();
	this.height = thumbnail.getHeight();
	// Padded row bytes = (width * bits per pixel + 31) / 32 * 4.
	this.bitsPerPixel = 24;
	this.numOfPlanes = 1;
	this.paddedRowBytes = (width*bitsPerPixel + 31)/32*4;
	// Total size = widthbytes * height * planes
	this.totalSize = paddedRowBytes*height*numOfPlanes;
	// Size after compression. Used for consistency check.
	this.compressedSize = data.length;
	this.dataType = Thumbnail.DATA_TYPE_KJpegRGB;
		
	return new IRBThumbnail(width, height, dataType, data);
}
 
Example 4
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();
  int size = Math.min(source.getWidth(), source.getHeight());

  mWidth = (source.getWidth() - size) / 2;
  mHeight = (source.getHeight() - size) / 2;

  Bitmap.Config config =
      source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
  Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, config);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(source, mWidth, mHeight, size, size);
  }

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 5
/**
 * 将彩色图转换为灰度图
 *
 * @param img 源Bitmap
 * @return 返回转换好的位图
 */
public static Bitmap convertGreyImg(Bitmap img) {
    int width = img.getWidth(); // 获取位图的宽
    int height = img.getHeight(); // 获取位图的高

    int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组

    img.getPixels(pixels, 0, width, 0, 0, width, height);
    int alpha = 0xFF << 24;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int grey = pixels[width * i + j];

            int red = ((grey & 0x00FF0000) >> 16);
            int green = ((grey & 0x0000FF00) >> 8);
            int blue = (grey & 0x000000FF);

            grey = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);
            grey = alpha | (grey << 16) | (grey << 8) | grey;
            pixels[width * i + j] = grey;
        }
    }
    Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565);
    result.setPixels(pixels, 0, width, 0, 0, width, height);
    return result;
}
 
Example 6
public static Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    float bitmapRatio = (float) width / (float) height;
    if (bitmapRatio > 1) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
    } else {
        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
}
 
Example 7
public int getBitmapHeight() {
    if (currentImage instanceof AnimatedFileDrawable) {
        return orientation % 360 == 0 || orientation % 360 == 180 ? currentImage.getIntrinsicHeight() : currentImage.getIntrinsicWidth();
    } else if (staticThumb instanceof AnimatedFileDrawable) {
        return orientation % 360 == 0 || orientation % 360 == 180 ? staticThumb.getIntrinsicHeight() : staticThumb.getIntrinsicWidth();
    }
    Bitmap bitmap = getBitmap();
    if (bitmap == null) {
        if (staticThumb != null) {
            return staticThumb.getIntrinsicHeight();
        }
        return 1;
    }
    return orientation % 360 == 0 || orientation % 360 == 180 ? bitmap.getHeight() : bitmap.getWidth();
}
 
Example 8
Source Project: iBeebo   File: BitmapUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * 压缩一个Bitmap
 */
public static Bitmap getScaleBitmap(Bitmap bitmap, float scale) {
    // 1,600px × 900px
    if (bitmap != null && bitmap.getWidth() > 1600 && bitmap.getHeight() > 900) {
        float sx = 1600 / (float) bitmap.getWidth();
        float sy = 900 / (float) bitmap.getHeight();
        Bitmap result = Bitmap.createBitmap(1600, 900, Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Matrix matrix = new Matrix();
        matrix.postScale(sx, sy);
        canvas.drawBitmap(bitmap, matrix, null);
        return result;
    }
    return bitmap;
}
 
Example 9
Source Project: 365browser   File: BitmapUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Crops a |bitmap| to a certain square |size|
 * @param bitmap The bitmap to crop.
 * @param size The size desired (width and height).
 * @return The resulting (square) bitmap.
 */
private static Bitmap cropToSquare(Bitmap bitmap, int size) {
    int x = 0;
    int y = 0;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width == size && height == size) return bitmap;

    if (width > size) x = (width - size) / 2;
    if (height > size) y = (height - size) / 2;
    return Bitmap.createBitmap(bitmap, x, y, size, size);
}
 
Example 10
Source Project: QiQuYing   File: ImgUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
	 * 按照宽度的百分比压缩
	 * 
	 * @param srcBitmap
	 * @param newHeight
	 * @return
	 */
	public static Bitmap bitmapZoomByHeight(Bitmap srcBitmap, int newHeight) {
//		int srcWidth = srcBitmap.getWidth();
		int srcHeight = srcBitmap.getHeight();

		float scaleHeight = ((float) newHeight) / srcHeight;
//		float scaleWidth = 1;

		return bitmapZoomByScale(srcBitmap, scaleHeight, scaleHeight);
	}
 
Example 11
Source Project: FreezeYou   File: LauncherShortcutUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * bm 和 icon 至少有一个不为 null,如均为非空则使用 bm
 *
 * @param title   Title
 * @param intent  Intent
 * @param icon    Icon
 * @param context Context
 * @param bm      Bitmap
 */
private static void requestCreateShortCutOldApi(String title, Intent intent, Drawable icon, Context context, Bitmap bm) {
    Intent addShortCut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
    addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    try {
        Bitmap bitmap;
        if (bm == null) {
            bitmap = getBitmapFromDrawable(icon);
        } else {
            bitmap = bm;
        }
        float size = context.getResources().getDimension(android.R.dimen.app_icon_size);
        if (size < 1) {
            size = 72;
        }
        if (bitmap.getHeight() > size) {
            Matrix matrix = new Matrix();
            float scaleWidth = (size) / bitmap.getWidth();
            float scaleHeight = (size) / bitmap.getHeight();
            matrix.postScale(scaleWidth, scaleHeight);
            addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON,
                    Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true));
            context.sendBroadcast(addShortCut);
        } else {
            addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
            context.sendBroadcast(addShortCut);
        }
        showToast(context, R.string.requested);
    } catch (Exception e) {
        e.printStackTrace();
        showToast(context, context.getString(R.string.requestFailed) + e.getMessage());
    }
}
 
Example 12
private Bitmap getBitmapWithText(int resourceId, String strNumber, int fontColor, float fontSize, float offsetY) {
	Bitmap bitmapBackground = BitmapFactory.decodeResource(mContext.getResources(), resourceId);

	int width = bitmapBackground.getWidth();
	int height = bitmapBackground.getHeight();
	//Log.i(LOG_TAG, "getBitmapWithText: width=" + width + ", height=" + height + ", density=" + bitmapBackground.getDensity());

	Bitmap textBitmap = Bitmap.createBitmap(width, height, BITMAP_CONFIG_DEFAULT);

	Canvas canvas = new Canvas(textBitmap);

	canvas.drawBitmap(bitmapBackground, 0, 0, null);

	// set font style
	mTextPaint.setColor(fontColor);
	// set font size
	mTextPaint.setTextSize(fontSize * mScaleFactor);
	// set font type
	if (POI_FONT_TYPEFACE != null) {
		mTextPaint.setTypeface(POI_FONT_TYPEFACE);
	}

	// get text offset		
	mTextPaint.getTextBounds(strNumber, 0, strNumber.length(), mTempRect);
	float offsetX = (width - mTempRect.width()) / 2 - mTempRect.left;
	if (offsetY == 0.0F) {
		offsetY = (height - mTempRect.height()) / 2 + mTempRect.height();
	} else {
		offsetY = offsetY * mScaleFactor + mTempRect.height();
	}

	//Log.i(LOG_TAG, "getBitmapWithText: number=" + number + ", focused=" + focused);
	//Log.i(LOG_TAG, "getBitmapWithText: offsetX=" + offsetX + ", offsetY=" + offsetY + ", boundsWidth=" + mTempRect.width() + ", boundsHeight=" + mTempRect.height());

	// draw text
	canvas.drawText(strNumber, offsetX, offsetY, mTextPaint);

	return textBitmap;
}
 
Example 13
public static Bitmap scaleBitmap(Bitmap bitmap, int width, int height) {
    int oHeight = bitmap.getHeight();
    int oWidth = bitmap.getWidth();
    int sqr = oHeight > oWidth ? oHeight : oWidth;

    Paint paint = new Paint();
    paint.setFilterBitmap(true);
    Bitmap background = Bitmap.createBitmap(sqr, sqr, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(background);
    canvas.drawBitmap(bitmap, (sqr - oWidth) / 2, (sqr - oHeight) / 2, paint);
    return Bitmap.createScaledBitmap(background, width, height, false);
}
 
Example 14
/**
 * This method of image pixelization utilizes the bitmap scaling operations built
 * into the framework. By downscaling the bitmap and upscaling it back to its
 * original size (while setting the filter flag to false), the same effect can be
 * achieved with much better performance.
 */
public BitmapDrawable builtInPixelization(float pixelizationFactor, Bitmap bitmap) {

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

    int downScaleFactorWidth = (int) (pixelizationFactor * width);
    downScaleFactorWidth = downScaleFactorWidth > 0 ? downScaleFactorWidth : 1;
    int downScaleFactorHeight = (int) (pixelizationFactor * height);
    downScaleFactorHeight = downScaleFactorHeight > 0 ? downScaleFactorHeight : 1;

    int downScaledWidth = width / downScaleFactorWidth;
    int downScaledHeight = height / downScaleFactorHeight;

    Bitmap pixelatedBitmap = Bitmap.createScaledBitmap(bitmap, downScaledWidth,
            downScaledHeight, false);

    /* Bitmap's createScaledBitmap method has a filter parameter that can be set to either
     * true or false in order to specify either bilinear filtering or point sampling
     * respectively when the bitmap is scaled up or now.
     *
     * Similarly, a BitmapDrawable also has a flag to specify the same thing. When the
     * BitmapDrawable is applied to an ImageView that has some scaleType, the filtering
     * flag is taken into consideration. However, for optimization purposes, this flag was
     * ignored in BitmapDrawables before Jelly Bean MR1.
     *
     * Here, it is important to note that prior to JBMR1, two bitmap scaling operations
     * are required to achieve the pixelization effect. Otherwise, a BitmapDrawable
     * can be created corresponding to the downscaled bitmap such that when it is
     * upscaled to fit the ImageView, the upscaling operation is a lot faster since
     * it uses internal optimizations to fit the ImageView.
     * */

    Bitmap upscaled = Bitmap.createScaledBitmap(pixelatedBitmap, width, height, false);
    return new BitmapDrawable(context.getResources(), upscaled);
}
 
Example 15
private void updateBitmap(Bitmap bitmap, int rotation) {
    float ps = width / bitmap.getWidth();
    scale *= ps;
    width = bitmap.getWidth();
    height = bitmap.getHeight();
    updateMinimumScale();
    float[] values = new float[9];
    matrix.getValues(values);
    matrix.reset();
    matrix.postScale(scale, scale);
    matrix.postTranslate(values[2], values[5]);
    updateMatrix();
}
 
Example 16
Source Project: v2ex   File: ImageUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取缩放的bitmap
 * @param context
 * @param imageResId
 * @param maxWidth
 * @param maxHeight
 * @return
 */
public static Bitmap getScaledBitmap(Context context, int imageResId, int maxWidth, int maxHeight) {
    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    Bitmap bitmap = null;
    // If we have to resize this image, first get the natural bounds.
    decodeOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(context.getResources(), imageResId, decodeOptions);
    int actualWidth = decodeOptions.outWidth;
    int actualHeight = decodeOptions.outHeight;
    Log.d(TAG, "Actual width: " + actualWidth + ", actual height: " + actualHeight);

    // Then compute the dimensions we would ideally like to decode to.
    int desiredWidth = getResizedDimension(maxWidth, maxHeight,
            actualWidth, actualHeight);
    int desiredHeight = getResizedDimension(maxHeight, maxWidth,
            actualHeight, actualWidth);
    Log.d(TAG, "Desired width: " + desiredWidth + ", desired height: " + desiredHeight);

    // Decode to the nearest power of two scaling factor.
    decodeOptions.inJustDecodeBounds = false;
    // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
    // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;
    decodeOptions.inSampleSize =
            findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
    Bitmap tempBitmap = BitmapFactory.decodeResource(context.getResources(), imageResId, decodeOptions);
    // If necessary, scale down to the maximal acceptable size.
    if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth ||
            tempBitmap.getHeight() > desiredHeight)) {
        bitmap = Bitmap.createScaledBitmap(tempBitmap,
                desiredWidth, desiredHeight, true);
        tempBitmap.recycle();
    } else {
        bitmap = tempBitmap;
    }
    return bitmap;
}
 
Example 17
/**
 * @return size in bytes of the underlying bitmap
 */
@Override
public int getSizeInBytes() {
  Bitmap bitmap = mBitmap;
  return (bitmap == null) ? 0 : bitmap.getHeight() * bitmap.getRowBytes();
}
 
Example 18
protected void splitBy( Bitmap bitmap, int height, int color, String chars ) {
	
	autoUppercase = chars.equals( LATIN_UPPER );
	int length = chars.length();
	
	int width = bitmap.getWidth();
	float vHeight = (float)height / bitmap.getHeight();
	
	int pos;
	
spaceMeasuring:
	for (pos=0; pos <  width; pos++) {
		for (int j=0; j < height; j++) {
			if (bitmap.getPixel( pos, j ) != color) {
				break spaceMeasuring;
			}
		}
	}
	add( ' ', new RectF( 0, 0, (float)pos / width, vHeight ) );
	
	for (int i=0; i < length; i++) {
		
		char ch = chars.charAt( i );
		if (ch == ' ') {
			continue;
		} else {
			
			boolean found;
			int separator = pos;
			
			do {
				if (++separator >= width) {
					break;
				}
				found = true;
				for (int j=0; j < height; j++) {
					if (bitmap.getPixel( separator, j ) != color) {
						found = false;
						break;
					}
				}
			} while (!found);
			
			add( ch, new RectF( (float)pos / width, 0, (float)separator / width, vHeight ) );
			pos = separator + 1;
		}
	}
	
	lineHeight = baseLine = height( frames.get( chars.charAt( 0 ) ) );
}
 
Example 19
public static Bitmap shotRecyclerView(RecyclerView view, int itemHeight) {
    RecyclerView.Adapter adapter = view.getAdapter();
    Bitmap bigBitmap = null;
    if (adapter != null) {
        int size = adapter.getItemCount();
        int height = 0;
        Paint paint = new Paint();
        int iHeight = 0;
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

        // Use 1/8th of the available memory for this memory cache.
        final int cacheSize = maxMemory / 8;
        LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
        for (int i = 0; i < size; i++) {
            RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
            adapter.onBindViewHolder(holder, i);
            holder.itemView.measure(
                    View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(itemHeight, View.MeasureSpec.EXACTLY));
            holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(),
                    holder.itemView.getMeasuredHeight());
            holder.itemView.setDrawingCacheEnabled(true);
            holder.itemView.buildDrawingCache();
            Bitmap drawingCache = holder.itemView.getDrawingCache();
            if (drawingCache != null) {
                bitmaCache.put(String.valueOf(i), drawingCache);
            }
            height += holder.itemView.getMeasuredHeight();
        }

        bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Config.ARGB_8888);
        Canvas bigCanvas = new Canvas(bigBitmap);
        Drawable lBackground = view.getBackground();
        if (lBackground instanceof ColorDrawable) {
            ColorDrawable lColorDrawable = (ColorDrawable) lBackground;
            int lColor = lColorDrawable.getColor();
            bigCanvas.drawColor(lColor);
        }

        for (int i = 0; i < size; i++) {
            Bitmap bitmap = bitmaCache.get(String.valueOf(i));
            bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);
            iHeight += bitmap.getHeight();
            bitmap.recycle();
        }
    }

    return bigBitmap;
}
 
Example 20
/**
 * generate a blurred bitmap from given one
 * 
 * referenced: http://incubator.quasimondo.com/processing/superfastblur.pde
 * 
 * @param original
 * @param radius
 * @return
 */
public static Bitmap getBlurredBitmap(final Bitmap original,
        final int radius) {
    if (radius < 1) {
        return null;
    }

    final int width = original.getWidth();
    final int height = original.getHeight();
    final int wm = width - 1;
    final int hm = height - 1;
    final int wh = width * height;
    final int div = radius + radius + 1;
    final int r[] = new int[wh];
    final int g[] = new int[wh];
    final int b[] = new int[wh];
    int rsum, gsum, bsum, x, y, i, p, p1, p2, yp, yi, yw;
    final int vmin[] = new int[Math.max(width, height)];
    final int vmax[] = new int[Math.max(width, height)];
    final int dv[] = new int[256 * div];
    for (i = 0; i < (256 * div); i++) {
        dv[i] = i / div;
    }

    final int[] blurredBitmap = new int[wh];
    original.getPixels(blurredBitmap, 0, width, 0, 0, width, height);

    yw = 0;
    yi = 0;

    for (y = 0; y < height; y++) {
        rsum = 0;
        gsum = 0;
        bsum = 0;
        for (i = -radius; i <= radius; i++) {
            p = blurredBitmap[yi + Math.min(wm, Math.max(i, 0))];
            rsum += (p & 0xff0000) >> 16;
            gsum += (p & 0x00ff00) >> 8;
            bsum += p & 0x0000ff;
        }
        for (x = 0; x < width; x++) {
            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];

            if (y == 0) {
                vmin[x] = Math.min(x + radius + 1, wm);
                vmax[x] = Math.max(x - radius, 0);
            }
            p1 = blurredBitmap[yw + vmin[x]];
            p2 = blurredBitmap[yw + vmax[x]];

            rsum += ((p1 & 0xff0000) - (p2 & 0xff0000)) >> 16;
            gsum += ((p1 & 0x00ff00) - (p2 & 0x00ff00)) >> 8;
            bsum += (p1 & 0x0000ff) - (p2 & 0x0000ff);
            yi++;
        }
        yw += width;
    }

    for (x = 0; x < width; x++) {
        rsum = gsum = bsum = 0;
        yp = -radius * width;
        for (i = -radius; i <= radius; i++) {
            yi = Math.max(0, yp) + x;
            rsum += r[yi];
            gsum += g[yi];
            bsum += b[yi];
            yp += width;
        }
        yi = x;
        for (y = 0; y < height; y++) {
            blurredBitmap[yi] = 0xff000000 | (dv[rsum] << 16)
                    | (dv[gsum] << 8) | dv[bsum];
            if (x == 0) {
                vmin[y] = Math.min(y + radius + 1, hm) * width;
                vmax[y] = Math.max(y - radius, 0) * width;
            }
            p1 = x + vmin[y];
            p2 = x + vmax[y];

            rsum += r[p1] - r[p2];
            gsum += g[p1] - g[p2];
            bsum += b[p1] - b[p2];

            yi += width;
        }
    }

    return Bitmap.createBitmap(blurredBitmap, width, height,
            Bitmap.Config.RGB_565);
}