|
@@ -4,6 +4,7 @@ import android.content.Context;
|
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.Bitmap;
|
|
|
import android.graphics.BitmapFactory;
|
|
import android.graphics.BitmapFactory;
|
|
|
import android.net.Uri;
|
|
import android.net.Uri;
|
|
|
|
|
+import android.os.Environment;
|
|
|
import android.text.TextUtils;
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
import com.miekir.common.utils.SizeTool;
|
|
import com.miekir.common.utils.SizeTool;
|
|
@@ -29,6 +30,8 @@ import io.reactivex.ObservableOnSubscribe;
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
import io.reactivex.schedulers.Schedulers;
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
|
|
|
|
|
|
+import static android.os.Environment.DIRECTORY_PICTURES;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* @author 詹子聪
|
|
* @author 詹子聪
|
|
|
* @date 2020/7/27 15:53
|
|
* @date 2020/7/27 15:53
|
|
@@ -47,11 +50,62 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
|
|
|
@Override
|
|
@Override
|
|
|
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
|
|
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
|
|
|
// 根据URI获取图片的base64字符串
|
|
// 根据URI获取图片的base64字符串
|
|
|
- String photoBase64 = Base64Tool.getBase64FromUri(context, uri);
|
|
|
|
|
- if (TextUtils.isEmpty(photoBase64)) {
|
|
|
|
|
- emitter.onError(new Exception("Empty image"));
|
|
|
|
|
|
|
+ String photoBase64 = null;
|
|
|
|
|
+ // 这个需要顺时针旋转90度才正常
|
|
|
|
|
+ Bitmap rotatedBitmap = ImageUtil.rotateBitmap90(ImageUtil.getBitmapFromUri(context, uri));
|
|
|
|
|
+
|
|
|
|
|
+ if (rotatedBitmap == null) {
|
|
|
|
|
+ photoBase64 = Base64Tool.getBase64FromUri(context, uri);
|
|
|
|
|
+ if (TextUtils.isEmpty(photoBase64)) {
|
|
|
|
|
+ emitter.onError(new Exception("Empty image"));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ emitter.onNext(photoBase64);
|
|
|
|
|
+ }
|
|
|
} else {
|
|
} else {
|
|
|
- emitter.onNext(photoBase64);
|
|
|
|
|
|
|
+ int max = rotatedBitmap.getWidth() > rotatedBitmap.getHeight() ? rotatedBitmap.getWidth() : rotatedBitmap.getHeight();
|
|
|
|
|
+ int multi = 1;
|
|
|
|
|
+ if (max > 1000) {
|
|
|
|
|
+ multi = max / 1000 + 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES) + "/" + System.currentTimeMillis() + ".jpg");
|
|
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 开始压缩图片
|
|
|
|
|
+ boolean saveCropSuccess = ImageUtil.saveBitmapFile(rotatedBitmap, file, multi);
|
|
|
|
|
+
|
|
|
|
|
+ rotatedBitmap.recycle();
|
|
|
|
|
+ if (saveCropSuccess) {
|
|
|
|
|
+ File compressedFile = new Compressor(context.getApplicationContext())
|
|
|
|
|
+ .setQuality(90)
|
|
|
|
|
+ .setDestinationDirectoryPath(file.getParentFile().getAbsolutePath())
|
|
|
|
|
+ .compressToFile(file, "cp_" + file.getName());
|
|
|
|
|
+
|
|
|
|
|
+ file.delete();
|
|
|
|
|
+ // 保存最终截图成功
|
|
|
|
|
+ // 根据URI获取图片的base64字符串
|
|
|
|
|
+ photoBase64 = Base64Tool.getBase64FromFilePath(compressedFile.getAbsolutePath());
|
|
|
|
|
+
|
|
|
|
|
+ if (TextUtils.isEmpty(photoBase64)) {
|
|
|
|
|
+ photoBase64 = Base64Tool.getBase64FromUri(context, uri);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (TextUtils.isEmpty(photoBase64)) {
|
|
|
|
|
+ emitter.onError(new Exception("Empty image"));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ emitter.onNext(photoBase64);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ photoBase64 = Base64Tool.getBase64FromUri(context, uri);
|
|
|
|
|
+
|
|
|
|
|
+ if (TextUtils.isEmpty(photoBase64)) {
|
|
|
|
|
+ emitter.onError(new Exception("Empty image"));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ emitter.onNext(photoBase64);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
@@ -258,8 +312,8 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
|
|
|
rotatedBitmap = croppedBmp;
|
|
rotatedBitmap = croppedBmp;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (file.getParentFile() == null || !file.getParentFile().exists()) {
|
|
|
|
|
- file.mkdirs();
|
|
|
|
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
}
|
|
}
|
|
|
int max = rotatedBitmap.getWidth() > rotatedBitmap.getHeight() ? rotatedBitmap.getWidth() : rotatedBitmap.getHeight();
|
|
int max = rotatedBitmap.getWidth() > rotatedBitmap.getHeight() ? rotatedBitmap.getWidth() : rotatedBitmap.getHeight();
|
|
|
int multi = 1;
|
|
int multi = 1;
|
|
@@ -289,8 +343,9 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
|
|
|
emitter.onNext(photoBase64);
|
|
emitter.onNext(photoBase64);
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
- emitter.onNext("Crop photo error");
|
|
|
|
|
|
|
+ emitter.onError(new Exception("Empty image"));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
/*.filter(base64String -> {
|
|
/*.filter(base64String -> {
|
|
@@ -332,6 +387,7 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
private int mWidth = SizeTool.SCREEN_WIDTH;
|
|
private int mWidth = SizeTool.SCREEN_WIDTH;
|
|
|
private int mHeight = SizeTool.SCREEN_HEIGHT;
|
|
private int mHeight = SizeTool.SCREEN_HEIGHT;
|
|
|
|
|
|