更新 'SrcJuMethod.js'

Ju
src48597962 2 years ago
parent 55cdf5edff
commit 3ca10d1b6b
  1. 111
      SrcJuMethod.js

@ -91,7 +91,10 @@ function banner(start, arr, data, cfg){
} }
//图片压缩 //图片压缩
function imageCompress(imgurl,fileid) { function imageCompress(imgurl,fileid) {
function compress(path, inSampleSize, topath) { function compress(path, topath) {
if (!path) {
return imgurl;
}
let tmpfile = "hiker://files/_cache/1.txt"; let tmpfile = "hiker://files/_cache/1.txt";
if (!fileExist(tmpfile)) { if (!fileExist(tmpfile)) {
writeFile(tmpfile, ''); writeFile(tmpfile, '');
@ -99,54 +102,47 @@ function imageCompress(imgurl,fileid) {
const Bitmap = android.graphics.Bitmap; const Bitmap = android.graphics.Bitmap;
const BitmapFactory = android.graphics.BitmapFactory; const BitmapFactory = android.graphics.BitmapFactory;
const FileOutputStream = java.io.FileOutputStream; const FileOutputStream = java.io.FileOutputStream;
let options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize || 2; let options1 = new BitmapFactory.Options();
options.inPurgeable = true; options1.inJustDecodeBounds = true;
let bitmap; options1.inPurgeable = true;
if (!path) {
return false; let bitmap1;
} if (typeof path === "object" && path.getClass) {
if (topath && typeof path === "object" && path.getClass) { bitmap1 = BitmapFactory.decodeStream(path, null, options);
bitmap = BitmapFactory.decodeStream(path, null, options);
closeMe(path);
} else { } else {
bitmap = BitmapFactory.decodeFile(path, options); bitmap1 = BitmapFactory.decodeFile(path, options);
topath = topath || path;
} }
let os = new FileOutputStream(topath); options1.inJustDecodeBounds = false;
let s = false; let size;
try { if(options1.outWidth>=options1.outHeight){
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os); size = options1.outWidth;
s = true; }else{
} catch (e) { size = options1.outHeight;
log(e.toString());
} }
os.flush(); if(size>720){
os.close(); let options2 = new BitmapFactory.Options();
return s; options2.inSampleSize = parseInt(size/720) || 2;
} options2.inPurgeable = true;
function getPicInfo(path){ let bitmap2;
const BitmapFactory = android.graphics.BitmapFactory; if (topath && typeof path === "object" && path.getClass) {
let options = new BitmapFactory.Options(); bitmap2 = BitmapFactory.decodeStream(path, null, options);
options.inJustDecodeBounds = true; closeMe(path);
options.inPurgeable = true; } else {
if (!path) { bitmap2 = BitmapFactory.decodeFile(path, options);
return { topath = topath || path;
outWidth: 0, }
outHeight: 0 let os = new FileOutputStream(topath);
}; try {
bitmap2.compress(Bitmap.CompressFormat.PNG, 100, os);
return "file://" + topath;
} catch (e) {
log(e.toString());
}
os.flush();
os.close();
} }
let bitmap; return imgurl;
if (typeof path === "object" && path.getClass) {
bitmap = BitmapFactory.decodeStream(path, null, options);
} else {
bitmap = BitmapFactory.decodeFile(path, options);
}
options.inJustDecodeBounds = false;
log(options);
return options;
//getPicInfo(f).outWidth
//getPicInfo(f).outHeight
} }
function getName(path) { function getName(path) {
const File = java.io.File; const File = java.io.File;
@ -155,27 +151,6 @@ function imageCompress(imgurl,fileid) {
let f = fetch(imgurl, { let f = fetch(imgurl, {
inputStream: true inputStream: true
}); });
let size; let newpath = "/storage/emulated/0/Android/data/com.example.hikerview/files/Documents/_cache/"+(fileid||"")+"_"+getName(imgurl);
let info = getPicInfo(f); return compress(f, newpath);
if(info.outWidth>=info.outHeight){
size = info.outWidth;
}else{
size = info.outHeight;
}
//log(size);
if(size>1080){
//log(parseInt(size/1080));
//let newpath = "/storage/emulated/0/Android/data/com.example.hikerview/files/Documents/_cache/"+(fileid||"")+"_"+getName(imgurl);
let newpath = "/storage/emulated/0/Android/data/com.example.hikerview/files/Documents/_cache/1.jpg";
/*
let ff = fetch(imgurl, {
inputStream: true
});
*/
compress(f, 8, newpath);
//if(r){
// return "file://" + newpath;
//}
}
return imgurl;
} }

Loading…
Cancel
Save