|
|
@@ -1,11 +1,22 @@
|
|
|
package com.kfzs.cfyl.media.util;
|
|
|
|
|
|
+import android.content.res.Configuration;
|
|
|
+import android.content.res.Resources;
|
|
|
import android.graphics.Bitmap;
|
|
|
import android.graphics.Canvas;
|
|
|
import android.graphics.Matrix;
|
|
|
import android.graphics.Paint;
|
|
|
+import android.graphics.Rect;
|
|
|
|
|
|
import com.kfzs.cfyl.media.bean.Size;
|
|
|
+import com.kfzs.cfyl.media.fragment.FgtDoodle;
|
|
|
+import com.kfzs.cfyl.share_library.util.LogUtil;
|
|
|
+import com.sheep.gamegroup.model.entity.Video;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -13,6 +24,125 @@ import com.kfzs.cfyl.media.bean.Size;
|
|
|
* realicing@sina.com
|
|
|
*/
|
|
|
public class BitmapUtil {
|
|
|
+ /**
|
|
|
+ * 把两个位图覆盖合成为一个位图,以底层位图的长宽为基准
|
|
|
+ * @param backBitmap 在底部的位图
|
|
|
+ * @param frontBitmap 盖在上面的位图
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Bitmap mergeBitmap(Bitmap backBitmap, Bitmap frontBitmap) {
|
|
|
+
|
|
|
+ if (backBitmap == null || backBitmap.isRecycled()
|
|
|
+ || frontBitmap == null || frontBitmap.isRecycled()) {
|
|
|
+ LogUtil.println("backBitmap=" + backBitmap + ";frontBitmap=" + frontBitmap);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Bitmap bitmap = backBitmap.copy(Bitmap.Config.ARGB_8888, true);
|
|
|
+ Canvas canvas = new Canvas(bitmap);
|
|
|
+ Rect baseRect = new Rect(0, 0, backBitmap.getWidth(), backBitmap.getHeight());
|
|
|
+ Rect frontRect = new Rect(0, 0, frontBitmap.getWidth(), frontBitmap.getHeight());
|
|
|
+ canvas.drawBitmap(frontBitmap, frontRect, baseRect, null);
|
|
|
+ return bitmap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把两个位图覆盖合成为一个位图,左右拼接
|
|
|
+ * @param leftBitmap
|
|
|
+ * @param rightBitmap
|
|
|
+ * @param isBaseMax 是否以宽度大的位图为准,true则小图等比拉伸,false则大图等比压缩
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Bitmap mergeBitmap_LR(Bitmap leftBitmap, Bitmap rightBitmap, boolean isBaseMax) {
|
|
|
+
|
|
|
+ if (leftBitmap == null || leftBitmap.isRecycled()
|
|
|
+ || rightBitmap == null || rightBitmap.isRecycled()) {
|
|
|
+ LogUtil.println("leftBitmap=" + leftBitmap + ";rightBitmap=" + rightBitmap);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ int height = 0; // 拼接后的高度,按照参数取大或取小
|
|
|
+ if (isBaseMax) {
|
|
|
+ height = leftBitmap.getHeight() > rightBitmap.getHeight() ? leftBitmap.getHeight() : rightBitmap.getHeight();
|
|
|
+ } else {
|
|
|
+ height = leftBitmap.getHeight() < rightBitmap.getHeight() ? leftBitmap.getHeight() : rightBitmap.getHeight();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 缩放之后的bitmap
|
|
|
+ Bitmap tempBitmapL = leftBitmap;
|
|
|
+ Bitmap tempBitmapR = rightBitmap;
|
|
|
+
|
|
|
+ if (leftBitmap.getHeight() != height) {
|
|
|
+ tempBitmapL = Bitmap.createScaledBitmap(leftBitmap, (int)(leftBitmap.getWidth()*1f/leftBitmap.getHeight()*height), height, false);
|
|
|
+ } else if (rightBitmap.getHeight() != height) {
|
|
|
+ tempBitmapR = Bitmap.createScaledBitmap(rightBitmap, (int)(rightBitmap.getWidth()*1f/rightBitmap.getHeight()*height), height, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 拼接后的宽度
|
|
|
+ int width = tempBitmapL.getWidth() + tempBitmapR.getWidth();
|
|
|
+
|
|
|
+ // 定义输出的bitmap
|
|
|
+ Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
+ Canvas canvas = new Canvas(bitmap);
|
|
|
+
|
|
|
+ // 缩放后两个bitmap需要绘制的参数
|
|
|
+ Rect leftRect = new Rect(0, 0, tempBitmapL.getWidth(), tempBitmapL.getHeight());
|
|
|
+ Rect rightRect = new Rect(0, 0, tempBitmapR.getWidth(), tempBitmapR.getHeight());
|
|
|
+
|
|
|
+ // 右边图需要绘制的位置,往右边偏移左边图的宽度,高度是相同的
|
|
|
+ Rect rightRectT = new Rect(tempBitmapL.getWidth(), 0, width, height);
|
|
|
+
|
|
|
+ canvas.drawBitmap(tempBitmapL, leftRect, leftRect, null);
|
|
|
+ canvas.drawBitmap(tempBitmapR, rightRect, rightRectT, null);
|
|
|
+ return bitmap;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把两个位图覆盖合成为一个位图,上下拼接
|
|
|
+ * @param topBitmap
|
|
|
+ * @param bottomBitmap
|
|
|
+ * @param isBaseMax 是否以高度大的位图为准,true则小图等比拉伸,false则大图等比压缩
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Bitmap mergeBitmap_TB(Bitmap topBitmap, Bitmap bottomBitmap, boolean isBaseMax) {
|
|
|
+
|
|
|
+ if (topBitmap == null || topBitmap.isRecycled()
|
|
|
+ || bottomBitmap == null || bottomBitmap.isRecycled()) {
|
|
|
+ LogUtil.println("topBitmap=" + topBitmap + ";bottomBitmap=" + bottomBitmap);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ int width = 0;
|
|
|
+ if (isBaseMax) {
|
|
|
+ width = topBitmap.getWidth() > bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();
|
|
|
+ } else {
|
|
|
+ width = topBitmap.getWidth() < bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();
|
|
|
+ }
|
|
|
+ Bitmap tempBitmapT = topBitmap;
|
|
|
+ Bitmap tempBitmapB = bottomBitmap;
|
|
|
+
|
|
|
+ if (topBitmap.getWidth() != width) {
|
|
|
+ tempBitmapT = Bitmap.createScaledBitmap(topBitmap, width, (int)(topBitmap.getHeight()*1f/topBitmap.getWidth()*width), false);
|
|
|
+ } else if (bottomBitmap.getWidth() != width) {
|
|
|
+ tempBitmapB = Bitmap.createScaledBitmap(bottomBitmap, width, (int)(bottomBitmap.getHeight()*1f/bottomBitmap.getWidth()*width), false);
|
|
|
+ }
|
|
|
+
|
|
|
+ int height = tempBitmapT.getHeight() + tempBitmapB.getHeight();
|
|
|
+
|
|
|
+ Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
+ Canvas canvas = new Canvas(bitmap);
|
|
|
+
|
|
|
+ Rect topRect = new Rect(0, 0, tempBitmapT.getWidth(), tempBitmapT.getHeight());
|
|
|
+ Rect bottomRect = new Rect(0, 0, tempBitmapB.getWidth(), tempBitmapB.getHeight());
|
|
|
+
|
|
|
+ Rect bottomRectT = new Rect(0, tempBitmapT.getHeight(), width, height);
|
|
|
+
|
|
|
+ canvas.drawBitmap(tempBitmapT, topRect, topRect, null);
|
|
|
+ canvas.drawBitmap(tempBitmapB, bottomRect, bottomRectT, null);
|
|
|
+ return bitmap;
|
|
|
+ }
|
|
|
+ public static Bitmap createBigLogo(Bitmap bmp, Size size, Bitmap bgBitmap) {
|
|
|
+ Bitmap bitmap = createBigLogo(bmp, size);
|
|
|
+ return mergeBitmap(bgBitmap, bitmap);
|
|
|
+ }
|
|
|
public static Bitmap createBigLogo(Bitmap bmp, Size size) {
|
|
|
Bitmap result = Bitmap.createBitmap(size.getWidth(), size.getHeight(), Bitmap.Config.ARGB_4444);
|
|
|
Canvas canvas = new Canvas(result);
|
|
|
@@ -32,4 +162,74 @@ public class BitmapUtil {
|
|
|
canvas.drawBitmap(bmp, matrix, paint);
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ public static File saveImage(Bitmap bmp, String dir, String fileName) {
|
|
|
+ File appDir = new File(dir);
|
|
|
+ if (!appDir.exists()) {
|
|
|
+ appDir.mkdir();
|
|
|
+ }
|
|
|
+ File file = new File(appDir, fileName);
|
|
|
+ try {
|
|
|
+ FileOutputStream fos = new FileOutputStream(file);
|
|
|
+ bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
|
|
|
+ fos.flush();
|
|
|
+ fos.close();
|
|
|
+ return file;
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public static void saveImage(Bitmap bitmap, File file) {
|
|
|
+ File appDir = file.getParentFile();
|
|
|
+ if (!appDir.exists()) {
|
|
|
+ appDir.mkdir();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ FileOutputStream fos = new FileOutputStream(file);
|
|
|
+ bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
|
|
|
+ fos.flush();
|
|
|
+ fos.close();
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Bitmap getDefaultBitmap(Resources resources, Video data) {
|
|
|
+ int a = G.WIDTH;
|
|
|
+ int b = G.HEIGHT;
|
|
|
+ if(data != null){
|
|
|
+ a = data.getWidth();
|
|
|
+ b = data.getHeight();
|
|
|
+ }
|
|
|
+ boolean needExchange = false;
|
|
|
+ if(resources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
|
|
|
+ LogUtil.println(FgtDoodle.class.getSimpleName(), "info", "landscape");
|
|
|
+ needExchange = b > a;
|
|
|
+ } else if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
|
|
|
+ LogUtil.println(FgtDoodle.class.getSimpleName(), "info", "portrait");
|
|
|
+ needExchange = a > b;
|
|
|
+ }
|
|
|
+ if(needExchange) {
|
|
|
+ int tmp = b;
|
|
|
+ b = a;
|
|
|
+ a = tmp;
|
|
|
+ }
|
|
|
+ return Bitmap.createBitmap(a, b, Bitmap.Config.ARGB_4444);
|
|
|
+ }
|
|
|
+
|
|
|
+ //释放bitmap
|
|
|
+ public static void recycle(Bitmap bitmap) {
|
|
|
+ if(bitmap != null && !bitmap.isRecycled()){
|
|
|
+ try {
|
|
|
+ bitmap.recycle();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|