直播視訊網站源碼,多媒體圖檔壓縮工具類相關的代碼
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);
}
}
以上就是 直播視訊網站源碼,多媒體圖檔壓縮工具類相關的代碼,更多内容歡迎關注之後的文章