android.media.ThumbnailUtils Java Examples

The following examples show how to use android.media.ThumbnailUtils. 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: SnippetArticleViewHolder.java    From AndroidChromium with Apache License 2.0 7 votes vote down vote up
private void fadeThumbnailIn(SnippetArticle snippet, Bitmap thumbnail) {
    mImageCallback = null;
    if (thumbnail == null) return; // Nothing to do, we keep the placeholder.

    // We need to crop and scale the downloaded bitmap, as the ImageView we set it on won't be
    // able to do so when using a TransitionDrawable (as opposed to the straight bitmap).
    // That's a limitation of TransitionDrawable, which doesn't handle layers of varying sizes.
    Resources res = mThumbnailView.getResources();
    int targetSize = res.getDimensionPixelSize(R.dimen.snippets_thumbnail_size);
    Bitmap scaledThumbnail = ThumbnailUtils.extractThumbnail(
            thumbnail, targetSize, targetSize, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

    // Store the bitmap to skip the download task next time we display this snippet.
    snippet.setThumbnailBitmap(scaledThumbnail);

    // Cross-fade between the placeholder and the thumbnail.
    Drawable[] layers = {mThumbnailView.getDrawable(),
            new BitmapDrawable(mThumbnailView.getResources(), scaledThumbnail)};
    TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
    mThumbnailView.setImageDrawable(transitionDrawable);
    transitionDrawable.startTransition(FADE_IN_ANIMATION_TIME_MS);
}
 
Example #2
Source File: CameraController.java    From KrGallery with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onInfo(MediaRecorder mediaRecorder, int what, int extra) {
    if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED || what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED || what == MediaRecorder.MEDIA_RECORDER_INFO_UNKNOWN) {
        MediaRecorder tempRecorder = recorder;
        recorder = null;
        if (tempRecorder != null) {
            tempRecorder.stop();
            tempRecorder.release();
        }
        if (onVideoTakeCallback != null) {
            final Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(recordedFile, MediaStore.Video.Thumbnails.MINI_KIND);
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    if (onVideoTakeCallback != null) {
                        onVideoTakeCallback.onFinishVideoRecording(bitmap);
                        onVideoTakeCallback = null;
                    }
                }
            });
        }
    }
}
 
Example #3
Source File: RoundableImageView.java    From OpenGraphView with Apache License 2.0 6 votes vote down vote up
@Override
public void setImageBitmap(Bitmap bm) {
    super.setImageBitmap(bm);
    if (bm == null) {
        mPaint.reset();
        mPaint.setColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
        invalidate();
        return;
    } else {
        mPaint.setColor(ContextCompat.getColor(getContext(), android.R.color.white));
    }

    Bitmap centerCroppedBitmap = ThumbnailUtils.extractThumbnail(bm, mSide, mSide);
    BitmapShader shader = new BitmapShader(centerCroppedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mPaint.setShader(shader);
}
 
Example #4
Source File: CommUtil.java    From Viewer with Apache License 2.0 6 votes vote down vote up
/**
 * 获取图片缩略图
 */
public static Bitmap getPictureImage(String urlPath) {
	Bitmap bitmap = null;
	BitmapFactory.Options options = new BitmapFactory.Options();
	options.inJustDecodeBounds = true;
	bitmap = BitmapFactory.decodeFile(urlPath, options);
	options.inJustDecodeBounds = false;
	int h = options.outHeight;
	int w = options.outWidth;
	int beWidth = w / 100;
	int beHeight = h / 80;
	int be = 1;
	if (beWidth < beHeight) {
		be = beWidth;
	} else {
		be = beHeight;
	}
	if (be <= 0) {
		be = 1;
	}
	options.inSampleSize = be;
	bitmap = BitmapFactory.decodeFile(urlPath, options);
	bitmap = ThumbnailUtils.extractThumbnail(bitmap, 100, 80,ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
	return bitmap;
}
 
Example #5
Source File: BitmapUtils.java    From Viewer with Apache License 2.0 6 votes vote down vote up
/**
 * 获取图片缩略图
 */
public static Bitmap getPictureImage(String urlPath) {
	Bitmap bitmap = null;
	BitmapFactory.Options options = new BitmapFactory.Options();
	options.inJustDecodeBounds = true;
	bitmap = BitmapFactory.decodeFile(urlPath, options);
	options.inJustDecodeBounds = false;
	int h = options.outHeight;
	int w = options.outWidth;
	int beWidth = w / 100;
	int beHeight = h / 80;
	int be = 1;
	if (beWidth < beHeight) {
		be = beWidth;
	} else {
		be = beHeight;
	}
	if (be <= 0) {
		be = 1;
	}
	options.inSampleSize = be;
	bitmap = BitmapFactory.decodeFile(urlPath, options);
	bitmap = ThumbnailUtils.extractThumbnail(bitmap, 100, 80,ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
	return bitmap;
}
 
Example #6
Source File: InterestsItemView.java    From delion with Apache License 2.0 6 votes vote down vote up
@Override
protected Drawable doInBackground(Void... voids) {
    // This is run on a background thread.
    try {
        // TODO(peconn): Replace this with something from the C++ Chrome stack.
        URL imageUrl = new URL(mUrl);
        InputStream in = imageUrl.openStream();

        Bitmap raw = BitmapFactory.decodeStream(in);
        int dimension = Math.min(raw.getHeight(), raw.getWidth());
        RoundedBitmapDrawable img = RoundedBitmapDrawableFactory.create(mResources,
                ThumbnailUtils.extractThumbnail(raw, dimension, dimension));
        img.setCircular(true);

        return img;
    } catch (IOException e) {
        Log.e(TAG, "Error downloading image: " + e.toString());
    }
    return null;
}
 
Example #7
Source File: CircleImageView.java    From MediaNotification with Apache License 2.0 6 votes vote down vote up
@Override
public void onDraw(Canvas canvas) {
    if (bitmap != null) {
        int size = Math.min(canvas.getWidth(), canvas.getHeight());
        if (size != this.size) {
            this.size = size;
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, size, size);

            RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);

            roundedBitmapDrawable.setCornerRadius(size / 2);
            roundedBitmapDrawable.setAntiAlias(true);

            bitmap = ImageUtils.drawableToBitmap(roundedBitmapDrawable);
        }

        canvas.drawBitmap(bitmap, 0, 0, paint);
    }
}
 
Example #8
Source File: CameraUtils.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
/** 创建缩略图,返回缩略图文件路径 */
//没实现
   public String createThumbnail(Bitmap source, String fileName){
       int oldW = source.getWidth();
       int oldH = source.getHeight();
       
       int w = Math.round((float)oldW/MAX_SIZES);  //MAX_SIZE为缩略图最大尺寸
       int h = Math.round((float)oldH/MAX_SIZES);
       
       int newW = 0;
       int newH = 0;
       
       if(w <= 1 && h <= 1){
           return saveBitmap(source, fileName);
       }
       
       int i = w > h ? w : h;  //获取缩放比例
       
       newW = oldW/i;
       newH = oldH/i;
       
       Bitmap imgThumb = ThumbnailUtils.extractThumbnail(source, newW, newH);  //关键代码!!
       
       return saveBitmap(imgThumb, fileName);  //注:saveBitmap方法为保存图片并返回路径的private方法
   }
 
Example #9
Source File: SnippetArticleViewHolder.java    From delion with Apache License 2.0 6 votes vote down vote up
private void fadeThumbnailIn(SnippetArticle snippet, Bitmap thumbnail) {
    mImageCallback = null;
    if (thumbnail == null) return; // Nothing to do, we keep the placeholder.

    // We need to crop and scale the downloaded bitmap, as the ImageView we set it on won't be
    // able to do so when using a TransitionDrawable (as opposed to the straight bitmap).
    // That's a limitation of TransitionDrawable, which doesn't handle layers of varying sizes.
    Resources res = mThumbnailView.getResources();
    int targetSize = res.getDimensionPixelSize(R.dimen.snippets_thumbnail_size);
    Bitmap scaledThumbnail = ThumbnailUtils.extractThumbnail(
            thumbnail, targetSize, targetSize, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

    // Store the bitmap to skip the download task next time we display this snippet.
    snippet.setThumbnailBitmap(scaledThumbnail);

    // Cross-fade between the placeholder and the thumbnail.
    Drawable[] layers = {mThumbnailView.getDrawable(),
            new BitmapDrawable(mThumbnailView.getResources(), scaledThumbnail)};
    TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
    mThumbnailView.setImageDrawable(transitionDrawable);
    transitionDrawable.startTransition(FADE_IN_ANIMATION_TIME_MS);
}
 
Example #10
Source File: Classifier.java    From Chinese-number-gestures-recognition with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private float[] getPixels(Bitmap bitmap) {


        int[] intValues = new int[IMAGE_SIZE * IMAGE_SIZE];
        float[] floatValues = new float[IMAGE_SIZE * IMAGE_SIZE * 3];

        if (bitmap.getWidth() != IMAGE_SIZE || bitmap.getHeight() != IMAGE_SIZE) {
            // rescale the bitmap if needed
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, IMAGE_SIZE, IMAGE_SIZE);
        }

        bitmap.getPixels(intValues,0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

        for (int i = 0; i < intValues.length; ++i) {
            final int val = intValues[i];
            floatValues[i * 3] = Color.red(val) / 255.0f;
            floatValues[i * 3 + 1] = Color.green(val) / 255.0f;
            floatValues[i * 3 + 2] = Color.blue(val) / 255.0f;
        }
        return floatValues;
    }
 
Example #11
Source File: BitmapDecoder.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String extractThumbnail(String videoPath, String thumbPath) {
    if (!AttachmentStore.isFileExist(thumbPath)) {
        Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Images.Thumbnails.MICRO_KIND);
        if (bitmap != null) {
            AttachmentStore.saveBitmap(bitmap, thumbPath, true);
            return thumbPath;
        }
    }
    return thumbPath;
}
 
Example #12
Source File: VideoRequestHandler.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Result load(Request request, int networkPolicy) throws IOException {
    Uri uri = request.uri;
    String path = uri.getPath();
    if (!TextUtils.isEmpty(path)) {
        Bitmap bm = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
        return new Result(bm, Picasso.LoadedFrom.DISK);
    }
    return null;
}
 
Example #13
Source File: AddContactActivity.java    From BaldPhone with Apache License 2.0 5 votes vote down vote up
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
    if (!cropped) {
        final int dimension = Math.min(resource.getWidth(), resource.getHeight());
        resource = ThumbnailUtils.extractThumbnail(resource, dimension, dimension);
    }
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    resource.compress(Bitmap.CompressFormat.JPEG, 30, stream);
    try {
        addFullSizePhoto(rawId, stream.toByteArray(), contentResolver);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #14
Source File: AppIconView.java    From MediaNotification with Apache License 2.0 5 votes vote down vote up
private Bitmap getRoundBitmap(@DrawableRes int drawable, int size) {
    Bitmap bitmap = ImageUtils.drawableToBitmap(ContextCompat.getDrawable(getContext(), drawable));
    bitmap = Bitmap.createBitmap(bitmap, bitmap.getWidth() / 6, bitmap.getHeight() / 6, (int) (0.666 * bitmap.getWidth()), (int) (0.666 * bitmap.getHeight()));
    bitmap = ThumbnailUtils.extractThumbnail(bitmap, size, size);

    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);

    roundedBitmapDrawable.setCornerRadius(size / 2);
    roundedBitmapDrawable.setAntiAlias(true);

    return ImageUtils.drawableToBitmap(roundedBitmapDrawable);
}
 
Example #15
Source File: BitmapUtils.java    From Viewer with Apache License 2.0 5 votes vote down vote up
/**
 * 获取视频缩略图
 */
public static Bitmap getVideoImage(String urlPath){
	Bitmap bitmap = null;
	bitmap = ThumbnailUtils.createVideoThumbnail(urlPath, MediaStore.Images.Thumbnails.MICRO_KIND);
	bitmap = ThumbnailUtils.extractThumbnail(bitmap, 100, 80,ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
	return bitmap;
}
 
Example #16
Source File: BitmapUtils.java    From CrawlerForReader with Apache License 2.0 5 votes vote down vote up
/**
 * 根据指定的图像路径和大小来获取缩略图
 * 此方法有两点好处:
 * 1. 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度,
 * 第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。
 * 2. 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使
 * 用这个工具生成的图像不会被拉伸。
 *
 * @param imagePath 图像的路径
 * @param width     指定输出图像的宽度
 * @param height    指定输出图像的高度
 * @return 生成的缩略图
 */
public static Bitmap getImageThumbnail(String imagePath, int width, int height) {
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    // 获取这个图片的宽和高,注意此处的bitmap为null
    bitmap = BitmapFactory.decodeFile(imagePath, options);
    BitmapUtils.recycler(bitmap);
    options.inJustDecodeBounds = false; // 设为 false
    // 计算缩放比
    int h = options.outHeight;
    int w = options.outWidth;
    int beWidth = w / width;
    int beHeight = h / height;
    int be = 1;
    if (beWidth < beHeight) {
        be = beWidth;
    } else {
        be = beHeight;
    }
    if (be <= 0) {
        be = 1;
    }
    options.inSampleSize = be;
    // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
    bitmap = BitmapFactory.decodeFile(imagePath, options);
    // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
    Bitmap thumb = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
    BitmapUtils.recycler(bitmap);
    return thumb;
}
 
Example #17
Source File: FermiPlayerUtils.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public static Bitmap createVideoThumbnail(String filePath, int width, int height) {
    Bitmap bp = createVideoThumbnail(filePath);
    if (bp != null) {
        return ThumbnailUtils.extractThumbnail(bp, width, height);
    }
    return bp;
}
 
Example #18
Source File: CommUtil.java    From Viewer with Apache License 2.0 5 votes vote down vote up
/**
 * 获取视频缩略图
 */
public static Bitmap getVideoImage(String urlPath){
	Bitmap bitmap = null;
	bitmap = ThumbnailUtils.createVideoThumbnail(urlPath, MediaStore.Images.Thumbnails.MICRO_KIND);
	bitmap = ThumbnailUtils.extractThumbnail(bitmap, 100, 80,ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
	return bitmap;
}
 
Example #19
Source File: CircleImageView.java    From Pasta-Music with Apache License 2.0 5 votes vote down vote up
@Override
public void onDraw(Canvas canvas) {
    Bitmap image = ImageUtils.drawableToBitmap(getDrawable());
    if (image != null) {
        int size = Math.min(getWidth(), getHeight());
        image = ThumbnailUtils.extractThumbnail(image, size, size);

        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), image);

        roundedBitmapDrawable.setCornerRadius(size / 2);
        roundedBitmapDrawable.setAntiAlias(true);

        canvas.drawBitmap(ImageUtils.drawableToBitmap(roundedBitmapDrawable), 0, 0, paint);
    }
}
 
Example #20
Source File: Thumbnail.java    From UMS-Interface with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 根据指定的图像路径和大小来获取缩略图
 * 此方法有两点好处:
 *     1. 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度,
 *        第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。
 *     2. 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使
 *        用这个工具生成的图像不会被拉伸。
 * @param imagePath 图像的路径
 * @param width 指定输出图像的宽度
 * @param height 指定输出图像的高度
 * @return 生成的缩略图
 */
private static Bitmap getImageThumbnail(String imagePath, int width, int height) {
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    // 获取这个图片的宽和高,注意此处的bitmap为null
    bitmap = BitmapFactory.decodeFile(imagePath, options);
    options.inJustDecodeBounds = false; // 设为 false
    // 计算缩放比
    int h = options.outHeight;
    int w = options.outWidth;
    int beWidth = w / width;
    int beHeight = h / height;
    int be = 1;
    if (beWidth < beHeight) {
        be = beWidth;
    } else {
        be = beHeight;
    }
    if (be <= 0) {
        be = 1;
    }
    options.inSampleSize = be;
    // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
    bitmap = BitmapFactory.decodeFile(imagePath, options);
    // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
    bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
            ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
    return bitmap;
}
 
Example #21
Source File: BaseImageDownloader.java    From candybar with Apache License 2.0 5 votes vote down vote up
private InputStream getVideoThumbnailStream(String filePath) {
    Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(
            filePath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
    if (bitmap != null) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0, bos);
        return new ByteArrayInputStream(bos.toByteArray());
    }
    return null;
}
 
Example #22
Source File: ImageUtil.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
public static String convertToBase64(Bitmap bitmap, int maxWidth, int maxHeight) {
        if (bitmap == null) {
            return null;
        }
        long startTime = System.currentTimeMillis();
        int[] targetSize = computeTargetSize(bitmap, maxWidth, maxHeight);
        // 10-100ms量级
        Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bitmap, targetSize[0], targetSize[1]);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.PNG, 100, baos);
        String result = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
//        L.d("ImageUtil.convertToBase64 cost %s ms", (System.currentTimeMillis() - startTime));
        return result;
    }
 
Example #23
Source File: CircleImageView.java    From ColorPickerDialog with Apache License 2.0 5 votes vote down vote up
@Override
public void onDraw(Canvas canvas) {
    Bitmap image = ImageUtils.drawableToBitmap(getDrawable());
    if (image != null) {
        int size = Math.min(getWidth(), getHeight());
        image = ThumbnailUtils.extractThumbnail(image, size, size);

        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), image);

        roundedBitmapDrawable.setCornerRadius(size / 2);
        roundedBitmapDrawable.setAntiAlias(true);

        canvas.drawBitmap(ImageUtils.drawableToBitmap(roundedBitmapDrawable), 0, 0, paint);
    }
}
 
Example #24
Source File: GalleryImageView.java    From YCGallery with Apache License 2.0 5 votes vote down vote up
private ImageView addThumbnail(Bitmap bitmap) {
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(thumbnailSize, thumbnailSize);
    lp.setMargins(10, 10, 10, 10);
    Bitmap image = ThumbnailUtils.extractThumbnail(bitmap, thumbnailSize, thumbnailSize);
    ImageView thumbnailView = createThumbnailView(lp, image);
    thumbnailsContainer.addView(thumbnailView);
    return thumbnailView;
}
 
Example #25
Source File: VideoThumbLoader.java    From LQRWeChat with MIT License 5 votes vote down vote up
private static Bitmap createVideoThumbnail(String vidioPath, int width,
                                           int height, int kind) {
    Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(vidioPath, kind);
    bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
            ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
    return bitmap;
}
 
Example #26
Source File: FragmentiGapMap.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
protected static Bitmap getCircleBitmap(Bitmap bm, boolean mineAvatar) {
    int sice;
    if (mineAvatar) {
        sice = Math.min((int) G.context.getResources().getDimension(R.dimen.dp10), (int) G.context.getResources().getDimension(R.dimen.dp10));
    } else {
        sice = Math.min((int) G.context.getResources().getDimension(R.dimen.dp32), (int) G.context.getResources().getDimension(R.dimen.dp32));
    }
    Bitmap bitmap = ThumbnailUtils.extractThumbnail(bm, sice, sice);
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setFilterBitmap(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#f23131"));
    canvas.drawOval(rectF, paint);

    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth((float) 4);
    if (mineAvatar) {
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    } else {
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    }
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
 
Example #27
Source File: Thumbnail.java    From UMS-Interface with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 根据指定的图像路径和大小来获取缩略图
 * 此方法有两点好处:
 *     1. 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度,
 *        第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。
 *     2. 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使
 *        用这个工具生成的图像不会被拉伸。
 * @param imagePath 图像的路径
 * @param width 指定输出图像的宽度
 * @param height 指定输出图像的高度
 * @return 生成的缩略图
 */
private static Bitmap getImageThumbnail(String imagePath, int width, int height) {
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    // 获取这个图片的宽和高,注意此处的bitmap为null
    bitmap = BitmapFactory.decodeFile(imagePath, options);
    options.inJustDecodeBounds = false; // 设为 false
    // 计算缩放比
    int h = options.outHeight;
    int w = options.outWidth;
    int beWidth = w / width;
    int beHeight = h / height;
    int be = 1;
    if (beWidth < beHeight) {
        be = beWidth;
    } else {
        be = beHeight;
    }
    if (be <= 0) {
        be = 1;
    }
    options.inSampleSize = be;
    // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
    bitmap = BitmapFactory.decodeFile(imagePath, options);
    // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
    bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
            ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
    return bitmap;
}
 
Example #28
Source File: AppIconView.java    From Cleaner with Apache License 2.0 5 votes vote down vote up
private Bitmap getRoundBitmap(@DrawableRes int drawable, int size) {
    Bitmap bitmap = ImageUtils.drawableToBitmap(ContextCompat.getDrawable(getContext(), drawable));
    bitmap = Bitmap.createBitmap(bitmap, bitmap.getWidth() / 6, bitmap.getHeight() / 6, (int) (0.666 * bitmap.getWidth()), (int) (0.666 * bitmap.getHeight()));
    bitmap = ThumbnailUtils.extractThumbnail(bitmap, size, size);

    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);

    roundedBitmapDrawable.setCornerRadius(size / 2);
    roundedBitmapDrawable.setAntiAlias(true);

    return ImageUtils.drawableToBitmap(roundedBitmapDrawable);
}
 
Example #29
Source File: BaseImageDownloader.java    From Android-Application-ZJB with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.FROYO)
private InputStream getVideoThumbnailStream(String filePath) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        Bitmap bitmap = ThumbnailUtils
                .createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
        if (bitmap != null) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.PNG, 0, bos);
            return new ByteArrayInputStream(bos.toByteArray());
        }
    }
    return null;
}
 
Example #30
Source File: GifCreateHelper.java    From GSYVideoPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 生成gif图
 *
 * @param file                    保存的文件路径,请确保文件夹目录已经创建
 * @param pics                    需要转化的bitmap本地路径集合
 * @param delay                   每一帧之间的延时
 * @param inSampleSize            采样率,越大图片越小,越大图片越模糊,需要处理的时长越短
 * @param scaleSize               缩减尺寸比例,对生成的截图进行缩减,越大图片越模糊,需要处理的时长越短
 * @param gsyVideoGifSaveListener 结果回调
 */
public void createGif(File file, List<String> pics, int delay, int inSampleSize, int scaleSize,
                      final GSYVideoGifSaveListener gsyVideoGifSaveListener) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    AnimatedGifEncoder localAnimatedGifEncoder = new AnimatedGifEncoder();
    localAnimatedGifEncoder.start(baos);//start
    localAnimatedGifEncoder.setRepeat(0);//设置生成gif的开始播放时间。0为立即开始播放
    localAnimatedGifEncoder.setDelay(delay);
    for (int i = 0; i < pics.size(); i++) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = inSampleSize;
        options.inJustDecodeBounds = true; // 先获取原大小
        BitmapFactory.decodeFile(pics.get(i), options);
        double w = (double) options.outWidth / scaleSize;
        double h = (double) options.outHeight / scaleSize;
        options.inJustDecodeBounds = false; // 获取新的大小
        Bitmap bitmap = BitmapFactory.decodeFile(pics.get(i), options);
        Bitmap pic = ThumbnailUtils.extractThumbnail(bitmap, (int) w, (int) h);
        localAnimatedGifEncoder.addFrame(pic);
        bitmap.recycle();
        pic.recycle();
        gsyVideoGifSaveListener.process(i + 1, pics.size());
    }
    localAnimatedGifEncoder.finish();//finish
    try {
        FileOutputStream fos = new FileOutputStream(file.getPath());
        baos.writeTo(fos);
        baos.flush();
        fos.flush();
        baos.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
        gsyVideoGifSaveListener.result(false, file);
        return;
    }
    gsyVideoGifSaveListener.result(true, file);
}