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

The following examples show how to use android.graphics.Bitmap#getConfig() . 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 File: GrayscaleTransformation.java    From glide-transformations with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: BitmapTools.java    From starcor.xul with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 3
Source File: GrayscaleTransformation.java    From AccountBook with GNU General Public License v3.0 6 votes vote down vote up
@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
Source File: NativeUtil.java    From bither-android with 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 5
Source File: StaticResource.java    From 365browser with 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 6
Source File: FrameSequence.java    From FrameSequenceDrawable with Apache License 2.0 5 votes vote down vote up
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 7
Source File: MusicService.java    From Orin with 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 8
Source File: ImageUtils.java    From Jide-Note with 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 9
Source File: DokitFrescoPostprocessor.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
/**
 * 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 10
Source File: CircleTransform.java    From YCAudioPlayer with 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 11
Source File: Service.java    From Twire with 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 12
Source File: GalleryBitmapPool.java    From medialibrary with 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 13
Source File: Service.java    From Pocket-Plays-for-Twitch with 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 14
Source File: MusicService.java    From Phonograph with 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 15
Source File: SoftWareCanvas.java    From sa-sdk-android with 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 16
Source File: MusicService.java    From Muzesto with GNU General Public License v3.0 4 votes vote down vote up
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 17
Source File: EditBitmapUtils.java    From iBeebo with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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 18
Source File: BitmapUtils.java    From react-native-sunmi-inner-printer with MIT License 4 votes vote down vote up
/**
 * 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 19
Source File: BubbleImageHelper.java    From sctalk with 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;
}
 
Example 20
Source File: BitmapUtils.java    From react-native-sunmi-inner-printer with MIT License 4 votes vote down vote up
/**
     * 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;
    }