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

The following examples show how to use android.graphics.Bitmap#getConfig() . 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 check out the related API usage on the sidebar.
Example 1
Source Project: 365browser   File: StaticResource.java    License: Apache License 2.0 6 votes vote down vote up
private static Bitmap decodeBitmap(Resources resources, int resId, int fitWidth,
        int fitHeight) {
    BitmapFactory.Options options = createOptions(resources, resId, fitWidth, fitHeight);
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, resId, options);

    if (bitmap == null) return null;
    if (bitmap.getConfig() == options.inPreferredConfig) return bitmap;

    Bitmap convertedBitmap = Bitmap.createBitmap(
            bitmap.getWidth(), bitmap.getHeight(), options.inPreferredConfig);
    Canvas canvas = new Canvas(convertedBitmap);
    canvas.drawBitmap(bitmap, 0, 0, null);
    bitmap.recycle();
    return convertedBitmap;
}
 
Example 2
Source Project: bither-android   File: NativeUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static void compressBitmap(Bitmap bit, int quality, String fileName,
		boolean optimize) {
	Log.d("native", "compress of native");
	if (bit.getConfig() != Config.ARGB_8888) {
		Bitmap result = null;

		result = Bitmap.createBitmap(bit.getWidth(), bit.getHeight(),
				Config.ARGB_8888);
		Canvas canvas = new Canvas(result);
		Rect rect = new Rect(0, 0, bit.getWidth(), bit.getHeight());
		canvas.drawBitmap(bit, null, rect, null);
		saveBitmap(result, quality, fileName, optimize);
		result.recycle();
	} else {
		saveBitmap(bit, quality, fileName, optimize);
	}

}
 
Example 3
@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.Config config =
      source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
  Bitmap bitmap = mBitmapPool.get(width, height, config);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(width, height, config);
  }

  Canvas canvas = new Canvas(bitmap);
  ColorMatrix saturation = new ColorMatrix();
  saturation.setSaturation(0f);
  Paint paint = new Paint();
  paint.setColorFilter(new ColorMatrixColorFilter(saturation));
  canvas.drawBitmap(source, 0, 0, paint);

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 4
public static Bitmap.Config getConfig(Bitmap bmp) {
	Config cfg = bmp.getConfig();
	if (cfg != null) {
		return cfg;
	}
	switch (bmp.getRowBytes() / bmp.getWidth()) {
	case 4: // 32bits rgba8888
		return Config.ARGB_8888;
	case 2: // 16bits rgb565
		return Config.RGB_565;
	case 1: // 8bit
		return Config.ALPHA_8;
	default:
		return defaultConfig;
	}
}
 
Example 5
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
                           @NonNull Bitmap toTransform, int outWidth, int outHeight) {
  int width = toTransform.getWidth();
  int height = toTransform.getHeight();

  Bitmap.Config config =
      toTransform.getConfig() != null ? toTransform.getConfig() : Bitmap.Config.ARGB_8888;
  Bitmap bitmap = pool.get(width, height, config);

  setCanvasBitmapDensity(toTransform, bitmap);

  Canvas canvas = new Canvas(bitmap);
  ColorMatrix saturation = new ColorMatrix();
  saturation.setSaturation(0f);
  Paint paint = new Paint();
  paint.setColorFilter(new ColorMatrixColorFilter(saturation));
  canvas.drawBitmap(toTransform, 0, 0, paint);

  return bitmap;
}
 
Example 6
Source Project: sa-sdk-android   File: SoftWareCanvas.java    License: Apache License 2.0 5 votes vote down vote up
private Bitmap drawOnSFCanvas(Bitmap bitmap) {
    if (VERSION.SDK_INT < 26 || bitmap.getConfig() != Config.HARDWARE) {
        return bitmap;
    }
    Bitmap sfBitmap = bitmap.copy(Config.ARGB_8888, false);
    this.bitmapWeakSet.add(sfBitmap);
    return sfBitmap;
}
 
Example 7
private static Bitmap copy(Bitmap bitmap) {
    Bitmap.Config config = bitmap.getConfig();
    if (config == null) {
        config = Bitmap.Config.RGB_565;
    }
    try {
        return bitmap.copy(config, false);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 8
public static Bitmap getResizedBitmap(Bitmap bm, int dpHeight, Context context) {
    try {
        Bitmap.Config mConfig = bm.getConfig() == null ? Bitmap.Config.ARGB_8888 : bm.getConfig();

        Bitmap resizedBitmap = bm.copy(mConfig, true);
        int heightPx = Service.dpToPixels(context, dpHeight);
        int widthPx = (int) ((1.0 * resizedBitmap.getWidth() / resizedBitmap.getHeight()) * (heightPx * 1.0));
        return getResizedBitmap(resizedBitmap, widthPx, heightPx);
    } catch (Exception e) {
        return null;
    }
}
 
Example 9
Source Project: medialibrary   File: GalleryBitmapPool.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Adds the given bitmap to the pool.
 * @return Whether the bitmap was added to the pool.
 */
public boolean put(Bitmap b) {
    if (b == null || b.getConfig() != Bitmap.Config.ARGB_8888) {
        return false;
    }
    SparseArrayBitmapPool pool = getPoolForDimensions(b.getWidth(), b.getHeight());
    if (pool == null) {
        b.recycle();
        return false;
    } else {
        return pool.put(b);
    }
}
 
Example 10
Source Project: Twire   File: Service.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static Bitmap getResizedBitmap(Bitmap bm, int dpHeight, Context context) {
    try {
        Bitmap.Config mConfig = bm.getConfig() == null ? Bitmap.Config.ARGB_8888 : bm.getConfig();

        Bitmap resizedBitmap = bm.copy(mConfig, true);
        int heightPx = Service.dpToPixels(context, dpHeight);
        int widthPx = (int) ((1.0 * resizedBitmap.getWidth() / resizedBitmap.getHeight()) * (heightPx * 1.0));
        return getResizedBitmap(resizedBitmap, widthPx, heightPx);
    } catch (Exception e) {
        return null;
    }
}
 
Example 11
Source Project: YCAudioPlayer   File: CircleTransform.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }

    Bitmap.Config config = source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
    Bitmap bitmap = Bitmap.createBitmap(size, size, config);
    //注意:一定要做判断,因为gif等图片不能够切圆形
    if(bitmap!=null){
        canvas = new Canvas(bitmap);
    }else {
        return squaredBitmap;
    }

    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);
    squaredBitmap.recycle();
    return bitmap;
}
 
Example 12
public long getFrame(int frameNr, Bitmap output, int previousFrameNr) {
    if (output == null || output.getConfig() != Bitmap.Config.ARGB_8888) {
        throw new IllegalArgumentException("Bitmap passed must be non-null and ARGB_8888");
    }
    if (mNativeState == 0) {
        throw new IllegalStateException("attempted to draw destroyed FrameSequenceState");
    }
    return nativeGetFrame(mNativeState, frameNr, output, previousFrameNr);
}
 
Example 13
/**
 * Copies the content of {@code sourceBitmap} to {@code destBitmap}. Both bitmaps must have the
 * same width and height. If their {@link Bitmap.Config} are identical, the memory is directly
 * copied. Otherwise, the {@code sourceBitmap} is drawn into {@code destBitmap}.
 */
private static void internalCopyBitmap(Bitmap destBitmap, Bitmap sourceBitmap) {
    if (destBitmap.getConfig() == sourceBitmap.getConfig()) {
        Bitmaps.copyBitmap(destBitmap, sourceBitmap);
    } else {
        // The bitmap configurations might be different when the source bitmap's configuration is
        // null, because it uses an internal configuration and the destination bitmap's configuration
        // is the FALLBACK_BITMAP_CONFIGURATION. This is the case for static images for animated GIFs.
        Canvas canvas = new Canvas(destBitmap);
        canvas.drawBitmap(sourceBitmap, 0, 0, null);
    }
}
 
Example 14
Source Project: Jide-Note   File: ImageUtils.java    License: MIT License 5 votes vote down vote up
/**
     * 添加文字到图片,类似水印文字。
     * 功能并不完善,修改字体颜色和大小,需要修改源代码
     * @param gContext
     * @param gResId
     * @param gText
     * @return
     */
    public static Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) {
        Resources resources = gContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

        Bitmap.Config bitmapConfig = bitmap.getConfig();
        // set default bitmap config if none
        if (bitmapConfig == null) {
            bitmapConfig = Bitmap.Config.ARGB_8888;
        }
        // resource bitmaps are imutable,
        // so we need to convert it to mutable one
        bitmap = bitmap.copy(bitmapConfig, true);

        Canvas canvas = new Canvas(bitmap);
        // new antialised Paint
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // text color - #808080
        paint.setColor(Color.rgb(127, 127, 127));
        // text size in pixels
        paint.setTextSize((int) (14 * scale*5));
        // text shadow
        paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(gText, 0, gText.length(), bounds);
//      int x = (bitmap.getWidth() - bounds.width()) / 2;
//      int y = (bitmap.getHeight() + bounds.height()) / 2;
        //draw  text  to the bottom
        int x = (bitmap.getWidth() - bounds.width())/10*9 ;
        int y = (bitmap.getHeight() + bounds.height())/10*9;
        canvas.drawText(gText, x , y, paint);

        return bitmap;
    }
 
Example 15
Source Project: Orin   File: MusicService.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static Bitmap copy(Bitmap bitmap) {
    Bitmap.Config config = bitmap.getConfig();
    if (config == null) {
        config = Bitmap.Config.RGB_565;
    }
    try {
        return bitmap.copy(config, false);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 16
/**
     * Decodes bitmap (maybe immutable) that keeps aspect-ratio and spans most within the bounds.
     */
    public Bitmap decodeBitmapByStream(InputStream is, Rect bounds, int width, int height) {
    	Log.i(TAG, "width = " + width + " , " + "height = " + height);
    	Bitmap bitmap = null;
    	try {
    		// TODO: Take max pixels allowed into account for calculation to avoid possible OOM.
//    		Rect bounds = getBitmapBounds(is, false);
    		int sampleSize = Math.max(bounds.width() / width, bounds.height() / height);
    		sampleSize = Math.min(sampleSize,
    				Math.max(bounds.width() / height, bounds.height() / width));
    		
    		BitmapFactory.Options options = new BitmapFactory.Options();
    		options.inSampleSize = Math.max(sampleSize, 1);
    		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    		
    		Log.i(TAG, "sampleSize = " + sampleSize + " , " + "options.inSampleSize = " + options.inSampleSize);
    		bitmap = BitmapFactory.decodeStream(is, null, options);//!!!!溢出
    	} catch (Exception e) {
    		Log.e(TAG, e.getMessage());
    	} finally {
    		closeStream(is);
    	}
    	
    	// Ensure bitmap in 8888 format, good for editing as well as GL compatible.
    	if ((bitmap != null) && (bitmap.getConfig() != Bitmap.Config.ARGB_8888)) {
    		Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    		bitmap.recycle();
    		bitmap = copy;
    	}
    	
    	if (bitmap != null) {
    		// Scale down the sampled bitmap if it's still larger than the desired dimension.
    		float scale = Math.min((float) width / bitmap.getWidth(),
    				(float) height / bitmap.getHeight());
    		scale = Math.max(scale, Math.min((float) height / bitmap.getWidth(),
    				(float) width / bitmap.getHeight()));
    		if (scale < 1) {
    			Matrix m = new Matrix();
    			m.setScale(scale, scale);
    			Bitmap transformed = createBitmap(
    					bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m);
    			bitmap.recycle();
    			return transformed;
    		}
    	}
    	return bitmap;
    }
 
Example 17
/**
 * Decodes bitmap (maybe immutable) that keeps aspect-ratio and spans most within the bounds.
 */
private Bitmap decodeBitmap(Uri uri, int width, int height) {
	Log.i(TAG, "width = " + width + " , " + "height = " + height);
    InputStream is = null;
    Bitmap bitmap = null;

    try {
        // TODO: Take max pixels allowed into account for calculation to avoid possible OOM.
        Rect bounds = getBitmapBounds(uri);
        int sampleSize = Math.max(bounds.width() / width, bounds.height() / height);
        sampleSize = Math.min(sampleSize,
                Math.max(bounds.width() / height, bounds.height() / width));

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = Math.max(sampleSize, 1);
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        is = context.getContentResolver().openInputStream(uri);
        Log.i(TAG, "sampleSize = " + sampleSize + " , " + "options.inSampleSize = " + options.inSampleSize);
        bitmap = BitmapFactory.decodeStream(is, null, options);//!!!!溢出
    } catch (Exception e) {
    } finally {
        closeStream(is);
    }

    // Ensure bitmap in 8888 format, good for editing as well as GL compatible.
    if ((bitmap != null) && (bitmap.getConfig() != Bitmap.Config.ARGB_8888)) {
        Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        bitmap.recycle();
        bitmap = copy;
    }

    if (bitmap != null) {
        // Scale down the sampled bitmap if it's still larger than the desired dimension.
        float scale = Math.min((float) width / bitmap.getWidth(),
                (float) height / bitmap.getHeight());
        scale = Math.max(scale, Math.min((float) height / bitmap.getWidth(),
                (float) width / bitmap.getHeight()));
        if (scale < 1) {
            Matrix m = new Matrix();
            m.setScale(scale, scale);
            Bitmap transformed = createBitmap(
                    bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m);
            bitmap.recycle();
            return transformed;
        }
    }
    return bitmap;
}
 
Example 18
private void updateMediaSession(final String what) {
    int playState = mIsSupposedToBePlaying
            ? PlaybackStateCompat.STATE_PLAYING
            : PlaybackStateCompat.STATE_PAUSED;

    if (what.equals(PLAYSTATE_CHANGED) || what.equals(POSITION_CHANGED)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSession.setPlaybackState(new PlaybackStateCompat.Builder()
                    .setState(playState, position(), 1.0f)
                    .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE |
                            PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                    .build());
        }
    } else if (what.equals(META_CHANGED) || what.equals(QUEUE_CHANGED)) {
        Bitmap albumArt = ImageLoader.getInstance().loadImageSync(TimberUtils.getAlbumArtUri(getAlbumId()).toString());
        if (albumArt != null) {

            Bitmap.Config config = albumArt.getConfig();
            if (config == null) {
                config = Bitmap.Config.ARGB_8888;
            }
            albumArt = albumArt.copy(config, false);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSession.setMetadata(new MediaMetadataCompat.Builder()
                    .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName())
                    .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, getAlbumArtistName())
                    .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName())
                    .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getTrackName())
                    .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration())
                    .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getQueuePosition() + 1)
                    .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getQueue().length)
                    .putString(MediaMetadataCompat.METADATA_KEY_GENRE, getGenreName())
                    .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,
                            mShowAlbumArtOnLockscreen ? albumArt : null)
                    .build());

            mSession.setPlaybackState(new PlaybackStateCompat.Builder()
                    .setState(playState, position(), 1.0f)
                    .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE |
                            PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                    .build());
        }
    }
}
 
Example 19
/**
 * Decodes bitmap that keeps aspect-ratio and spans most within the bounds.
 */
private Bitmap decodeBitmap(Uri uri, int width, int height) {
    InputStream is = null;
    Bitmap bitmap = null;

    try {
        // TODO: Take max pixels allowed into account for calculation to avoid possible OOM.
        Rect bounds = getBitmapBounds(uri);
        int sampleSize = Math.max(bounds.width() / width, bounds.height() / height);
        sampleSize = Math.min(sampleSize, Math.max(bounds.width() / height, bounds.height() / width));

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = Math.max(sampleSize, 1);
        options.inPreferredConfig = Config.ARGB_8888;

        is = context.getContentResolver().openInputStream(uri);
        bitmap = BitmapFactory.decodeStream(is, null, options);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "FileNotFoundException: " + uri);
    } finally {
        closeStream(is);
    }

    // Ensure bitmap in 8888 format, good for editing as well as GL compatible.
    if ((bitmap != null) && (bitmap.getConfig() != Config.ARGB_8888)) {
        Bitmap copy = bitmap.copy(Config.ARGB_8888, true);
        bitmap.recycle();
        bitmap = copy;
    }

    if (bitmap != null) {
        // Scale down the sampled bitmap if it's still larger than the desired dimension.
        float scale = Math.min((float) width / bitmap.getWidth(), (float) height / bitmap.getHeight());
        scale = Math.max(scale, Math.min((float) height / bitmap.getWidth(), (float) width / bitmap.getHeight()));
        if (scale < 1) {
            Matrix m = new Matrix();
            m.setScale(scale, scale);
            Bitmap transformed = createBitmap(bitmap, m);
            bitmap.recycle();
            return transformed;
        }
    }
    return bitmap;
}
 
Example 20
Source Project: sctalk   File: BubbleImageHelper.java    License: Apache License 2.0 4 votes vote down vote up
public Bitmap getBubbleImageBitmap(Bitmap srcBitmap, int backgroundResourceID) {
    if (null == srcBitmap) {
        return null;
    }
    Bitmap background = null;
    background = BitmapFactory.decodeResource(context.getResources(),
            backgroundResourceID);
    if (null == background) {
        return null;
    }

    Bitmap mask = null;
    Bitmap newBitmap = null;
    mask = srcBitmap;
    resetSrcImgSize(srcBitmap);
    
    Bitmap tmp = getScaleImage(background, srcBitmap.getWidth(), srcBitmap.getHeight());
 if (null != tmp) {
 	background = tmp;
 } else {
 	tmp = getScaleImage(srcBitmap, srcBitmap.getWidth(),srcBitmap.getHeight());
 	if (null != tmp) {
 		mask = tmp;
 	}
 }    
    
    Config config = background.getConfig();
    if (null == config) {
        config = Bitmap.Config.ARGB_8888;
    }

    newBitmap = Bitmap.createBitmap(background.getWidth(),
            background.getHeight(), config);
    Canvas newCanvas = new Canvas(newBitmap);

    newCanvas.drawBitmap(background, 0, 0, null);

    Paint paint = new Paint();

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
    newCanvas.drawBitmap(mask, new Rect(0, 0, mask.getWidth(),mask.getHeight()),
            new Rect(0, 0, background.getWidth(), background.getHeight()),
            paint);

    return newBitmap;
}