Java Code Examples for android.graphics.Bitmap.CompressFormat#JPEG

The following examples show how to use android.graphics.Bitmap.CompressFormat#JPEG . 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: ImageUtil.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public static File getScaledImageFileWithMD5(File imageFile, String mimeType) {
    String filePath = imageFile.getPath();

    if (!isInvalidPictureFile(mimeType)) {
        return null;
    }

    String tempFilePath = getTempFilePath(FileUtil.getExtensionName(filePath));
    Log.i("ImageUtil", "tempFilePath:"+tempFilePath);
    File tempImageFile = AttachmentStore.create(tempFilePath);
    if (tempImageFile == null) {
        return null;
    }

    CompressFormat compressFormat = CompressFormat.JPEG;
    // 压缩数值由第三方开发者自行决定
    int maxWidth = 720;
    int quality = 60;

    if (ImageUtil.scaleImage(imageFile, tempImageFile, maxWidth, compressFormat, quality)) {
        return tempImageFile;
    } else {
        return null;
    }
}
 
Example 2
Source File: ImageUtil.java    From NIM_Android_UIKit with MIT License 6 votes vote down vote up
public static File getScaledImageFileWithMD5(File imageFile, String mimeType) {
    String filePath = imageFile.getPath();

    if (!isInvalidPictureFile(mimeType)) {
        LogUtil.i("ImageUtil", "is invalid picture file");
        return null;
    }

    String tempFilePath = getTempFilePath(FileUtil.getExtensionName(filePath));
    File tempImageFile = AttachmentStore.create(tempFilePath);
    if (tempImageFile == null) {
        return null;
    }

    CompressFormat compressFormat = CompressFormat.JPEG;
    // 压缩数值由第三方开发者自行决定
    int maxWidth = 720;
    int quality = 60;

    if (ImageUtil.scaleImage(imageFile, tempImageFile, maxWidth, compressFormat, quality)) {
        return tempImageFile;
    } else {
        return null;
    }
}
 
Example 3
Source File: FetcherHolder.java    From bither-bitmap-sample with Apache License 2.0 6 votes vote down vote up
private static void addImageCacheToFetcher(ImageFetcher fetcher) {
    ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams(
            BitherApplication.mContext, PI_IMAGE_CACHE_DIR);
    cacheParams.compressFormat = CompressFormat.JPEG;
    cacheParams.compressQuality = 100;
    cacheParams.diskCacheSize = 100 * 1024 * 1024;
    // Set memory cache to 25% of mem class
    cacheParams.setMemCacheSizePercent(BitherApplication.mContext, 0.25f);
    if (IMAGE_CACHE == null) {
        fetcher.addImageCache(cacheParams, CHECK_JOURNAL);
        IMAGE_CACHE = fetcher.getImageCache();
    } else {
        fetcher.setImageCache(IMAGE_CACHE);
        // because we can only use addImageCache to init Disk Cache, so here
        // we call it again
        fetcher.addImageCache(cacheParams, CHECK_JOURNAL);
    }
    CHECK_JOURNAL = false;
}
 
Example 4
Source File: BitmapUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static @NonNull CompressFormat getCompressFormatForContentType(@Nullable String contentType) {
  if (contentType == null) return CompressFormat.JPEG;

  switch (contentType) {
    case MediaUtil.IMAGE_JPEG: return CompressFormat.JPEG;
    case MediaUtil.IMAGE_PNG:  return CompressFormat.PNG;
    case MediaUtil.IMAGE_WEBP: return CompressFormat.WEBP;
    default:                   return CompressFormat.JPEG;
  }
}
 
Example 5
Source File: AndroidImageBridge.java    From CrossMobile with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void fillStreamAndClose(NativeBitmap nativebitmap, ImageType type, double quality, OutputStream out) throws IOException {
    Bitmap bitmap = ((AndroidBitmap) nativebitmap).bitmap;
    CompressFormat compress = type == ImageType.JPEG ? CompressFormat.JPEG : CompressFormat.PNG;
    try {
        if (!bitmap.compress(compress, (int) (quality * 100), out))
            throw new IOException("Unable to compress image with type " + type.name());
    } finally {
        closeR(out);
    }
}
 
Example 6
Source File: ImageUtil.java    From MyBlogDemo with Apache License 2.0 5 votes vote down vote up
/**
 * 判断图片类型
 *
 * @param b
 * @return
 */
public CompressFormat getImageType(byte[] b) {
    if (b[1] == (byte) 'P' && b[2] == (byte) 'N' && b[3] == (byte) 'G') {
        return CompressFormat.PNG;
    } else if (b[6] == (byte) 'J' && b[7] == (byte) 'F' && b[8] == (byte) 'I' && b[9] == (byte) 'F') {
        return CompressFormat.JPEG;
    } else {
        return CompressFormat.JPEG;
    }
}
 
Example 7
Source File: ImageUtil.java    From MyBlogDemo with Apache License 2.0 5 votes vote down vote up
/**
 * 通过url或文件名来判断图片类型
 *
 * @param url
 * @return
 */
public static CompressFormat getImageType(String url) {
    String name = url.substring(url.lastIndexOf(".") + 1);
    if (name.equalsIgnoreCase("png")) {
        return CompressFormat.PNG;
    } else if (name.equalsIgnoreCase("jpg")) {
        return CompressFormat.JPEG;
    } else {
        return CompressFormat.JPEG;
    }
}
 
Example 8
Source File: ImageDiskLruCache.java    From android-tv-launcher with MIT License 5 votes vote down vote up
/**
 * 根据文件类型获得CompressFormat.
 *
 * @Description:
 * @Date 2014-3-7
 */
private CompressFormat getCompressFormat(String url) {
    String lowerUrl = url.toLowerCase(Locale.ENGLISH);
    if (lowerUrl.endsWith(".jpg")) {
        return CompressFormat.JPEG;
    } else if (lowerUrl.endsWith(".png")) {
        return CompressFormat.PNG;
    }
    return CompressFormat.JPEG;
}
 
Example 9
Source File: CropActivity.java    From imageCrop with MIT License 4 votes vote down vote up
protected static CompressFormat convertExtensionToCompressFormat(String extension) {
    return extension.equals("png") ? CompressFormat.PNG : CompressFormat.JPEG;
}
 
Example 10
Source File: WallpaperCropActivity.java    From TurboLauncher with Apache License 2.0 4 votes vote down vote up
protected static CompressFormat convertExtensionToCompressFormat(String extension) {
    return extension.equals("png") ? CompressFormat.PNG : CompressFormat.JPEG;
}
 
Example 11
Source File: BitmapUtil.java    From Silence with GNU General Public License v3.0 4 votes vote down vote up
public static <T> byte[] createScaledBytes(Context context, T model, MediaConstraints constraints)
    throws BitmapDecodingException
{
  CompressFormat compressFormat = CompressFormat.PNG;

  int    quality  = 100;
  int    attempts = 0;
  byte[] bytes;

  Bitmap scaledBitmap =  Downsampler.AT_MOST.decode(getInputStreamForModel(context, model),
                                                    Glide.get(context).getBitmapPool(),
                                                    constraints.getImageMaxWidth(context),
                                                    constraints.getImageMaxHeight(context),
                                                    DecodeFormat.PREFER_RGB_565);

  if (scaledBitmap == null) {
    throw new BitmapDecodingException("Unable to decode image");
  }
  
  try {
    do {
      attempts++;
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      scaledBitmap.compress(compressFormat, quality, baos);
      bytes = baos.toByteArray();

      Log.w(TAG, "iteration with quality " + quality + "; size " + (bytes.length / 1024) + "kb; attempts " + attempts);

      compressFormat = CompressFormat.JPEG;

      if (quality > MAX_COMPRESSION_QUALITY) {
        quality = MAX_COMPRESSION_QUALITY;
      } else {
        quality = quality - COMPRESSION_QUALITY_DECREASE;
      }
    }
    while (bytes.length > constraints.getImageMaxSize(context));
    return bytes;
  } finally {
    if (scaledBitmap != null) scaledBitmap.recycle();
  }
}
 
Example 12
Source File: WallpaperCropActivity.java    From LB-Launcher with Apache License 2.0 4 votes vote down vote up
protected static CompressFormat convertExtensionToCompressFormat(String extension) {
    return extension.equals("png") ? CompressFormat.PNG : CompressFormat.JPEG;
}