直播视频网站源码,多媒体图片压缩工具类

简介: 直播视频网站源码,多媒体图片压缩工具类

直播视频网站源码,多媒体图片压缩工具类相关的代码

public class MediaUtils {

    private MediaUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * @param bMute 值为true时为关闭背景音乐。
     */
    @TargetApi(Build.VERSION_CODES.FROYO) public static boolean muteAudioFocus(Context context,
        boolean bMute) {
        boolean bool = false;
        AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (bMute) {
            int result =
                am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
        } else {
            int result = am.abandonAudioFocus(null);
            bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
        }
        return bool;
    }

    /*
     * 3.质量压缩
     * 设置bitmap options属性,降低图片的质量,像素不会减少
     * 第一个参数为需要压缩的bitmap图片对象,第二个参数为压缩后图片保存的位置
     * 设置options 属性0-100,来实现压缩
     *
     * @param bmp
     * @param file
     */
    public static void qualityCompress(String imgPath, String outImg) {
        File file = new File(outImg);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(imgPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int options = 20;
        bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
        while (baos.toByteArray().length / 1024 > 190 && options > 5) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩

            baos.reset(); // 重置baos即清空baos
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
            options -= 5;// 每次都减少5
        }
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baos.toByteArray());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 质量压缩方法
     */
    public static void compressImage(Bitmap image, String outImg) {
        File file = new File(outImg);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
        /*int options = 90;

        while (baos.toByteArray().length / 1024 > 200 && options >= 10) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset(); // 重置baos即清空baos
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
            options -= 10;// 每次都减少10
            LogUtil.e("图片压缩中", "");
            LogUtil.e("options:", "" + options);
        }*/

        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baos.toByteArray());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Compress image by size, this will modify image width/height.
     * Used to get thumbnail
     *
     * @param pixelW target pixel of width
     * @param pixelH target pixel of height
     */
    public static void ratio(String imgPath, String outImg, float pixelW, float pixelH) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(imgPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Bitmap image = BitmapFactory.decodeStream(fis);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, os);
        //判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
        if (os.toByteArray().length / 1024 > 180) {
            os.reset();//重置baos即清空baos
            image.compress(Bitmap.CompressFormat.JPEG, 90, os);//这里压缩50%,把压缩后的数据存放到baos中
        }
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        //开始读入图片,此时把options.inJustDecodeBounds 设回true了
        newOpts.inJustDecodeBounds = true;
        newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bitmap = BitmapFactory.decodeStream(is, null, newOpts);
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了
        float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了
        //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
        int be = 1;//be=1表示不缩放
        if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0) be = 1;
        newOpts.inSampleSize = be;//设置缩放比例
        //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
        is = new ByteArrayInputStream(os.toByteArray());
        bitmap = BitmapFactory.decodeStream(is, null, newOpts);
        //压缩好比例大小后再进行质量压缩
        compressImage(bitmap, outImg);
    }
}

以上就是 直播视频网站源码,多媒体图片压缩工具类相关的代码,更多内容欢迎关注之后的文章

目录
相关文章
|
5月前
|
小程序 JavaScript 开发工具
Uniapp 对接抖音短剧播放器 video-player 坑点解决
Uniapp 对接抖音短剧播放器 video-player 坑点解决
298 1
|
5月前
多功能在线二维码生成源码
上传即可使用,可以把电子名片、文本、wifi网络、电子邮件、短信、电话号码、网址等信息生成对应的二维码图片。
97 24
多功能在线二维码生成源码
探究直播app源码技术:视频上传功能
我今天要讲的技术功能就有助于我们的情绪发泄,是直播app源码平台中的技术功能。在我们使用直播app源码平台的时候,我们有一个最常见且可能最先看到的一个功能,它是有很多能动的画面,各式各样的内容,并且还可以上下滑动切换这些画面
探究直播app源码技术:视频上传功能
uniapp 微信语音播放功能(整理)
uniapp 微信语音播放功能(整理)
直播源码app开发技术之特效功能的实现
想必说到这里大家都明白我说的这一个情况是直播源码app平台的哪一个常见的功能了,没错,这个功能就是直播源码app平台的特效功能,这个功能对于开发直播源码app平台是非常重要的,废话不多说,下面我就为大家分享直播源码app开发技术特效功能的实现。
直播源码app开发技术之特效功能的实现
|
监控 安全 黑灰产治理
直播软件源码,审核的“中坚力量”鉴黄功能!
在利用直播软件源码去开发平台中,直播软件源码功能技术是开发直播平台的重要技术之一,今天我就为大家分享直播软件源码技术技术中的鉴黄功能的实现。
直播软件源码,审核的“中坚力量”鉴黄功能!
|
编解码 Java API
CSDN直播BlOB视频流下载,JAVA实现下载直播视频
CSDN直播BlOB视频流下载,JAVA实现下载直播视频
451 0
CSDN直播BlOB视频流下载,JAVA实现下载直播视频
|
缓存 UED
聊一聊播放器在一对一直播系统源码中的作用
建立视频数据缓冲区,当网络不足以支持一对一直播系统源码用户流畅的观看直播时,系统会暂停播放,缓存一定的数据,支撑用户流畅观看。
直播短视频源码中不容易引人注目但很实用的小功能
随着某音某手的发布,短视频似乎已经默认成为了我们生活的一部分,他给我们带来的不仅仅是消磨时光,更多的正能量内容也在潜移默化的影响着我们,而且在直播短视频源码的开发中,有很多不容易引人注目但却很实用的小功能你知道吗?
直播短视频源码中不容易引人注目但很实用的小功能
短视频APP开发,调用系统相册,选择音频、视频、图片
短视频APP开发,调用系统相册,选择音频、视频、图片
288 0
下一篇
无影云桌面