From 3ca10d1b6b928926cc4c9f94c396e16b702b15d0 Mon Sep 17 00:00:00 2001 From: src48597962 <48597962@qq.com> Date: Fri, 4 Aug 2023 16:11:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'SrcJuMethod.js'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SrcJuMethod.js | 111 +++++++++++++++++++------------------------------ 1 file changed, 43 insertions(+), 68 deletions(-) diff --git a/SrcJuMethod.js b/SrcJuMethod.js index 89e084cb..b008961f 100644 --- a/SrcJuMethod.js +++ b/SrcJuMethod.js @@ -91,7 +91,10 @@ function banner(start, arr, data, cfg){ } //图片压缩 function imageCompress(imgurl,fileid) { - function compress(path, inSampleSize, topath) { + function compress(path, topath) { + if (!path) { + return imgurl; + } let tmpfile = "hiker://files/_cache/1.txt"; if (!fileExist(tmpfile)) { writeFile(tmpfile, ''); @@ -99,54 +102,47 @@ function imageCompress(imgurl,fileid) { const Bitmap = android.graphics.Bitmap; const BitmapFactory = android.graphics.BitmapFactory; const FileOutputStream = java.io.FileOutputStream; - let options = new BitmapFactory.Options(); - options.inSampleSize = inSampleSize || 2; - options.inPurgeable = true; - let bitmap; - if (!path) { - return false; - } - if (topath && typeof path === "object" && path.getClass) { - bitmap = BitmapFactory.decodeStream(path, null, options); - closeMe(path); + + let options1 = new BitmapFactory.Options(); + options1.inJustDecodeBounds = true; + options1.inPurgeable = true; + + let bitmap1; + if (typeof path === "object" && path.getClass) { + bitmap1 = BitmapFactory.decodeStream(path, null, options); } else { - bitmap = BitmapFactory.decodeFile(path, options); - topath = topath || path; + bitmap1 = BitmapFactory.decodeFile(path, options); } - let os = new FileOutputStream(topath); - let s = false; - try { - bitmap.compress(Bitmap.CompressFormat.PNG, 100, os); - s = true; - } catch (e) { - log(e.toString()); + options1.inJustDecodeBounds = false; + let size; + if(options1.outWidth>=options1.outHeight){ + size = options1.outWidth; + }else{ + size = options1.outHeight; } - os.flush(); - os.close(); - return s; - } - function getPicInfo(path){ - const BitmapFactory = android.graphics.BitmapFactory; - let options = new BitmapFactory.Options(); - options.inJustDecodeBounds = true; - options.inPurgeable = true; - if (!path) { - return { - outWidth: 0, - outHeight: 0 - }; + if(size>720){ + let options2 = new BitmapFactory.Options(); + options2.inSampleSize = parseInt(size/720) || 2; + options2.inPurgeable = true; + let bitmap2; + if (topath && typeof path === "object" && path.getClass) { + bitmap2 = BitmapFactory.decodeStream(path, null, options); + closeMe(path); + } else { + bitmap2 = BitmapFactory.decodeFile(path, options); + topath = topath || path; + } + 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; - 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 + return imgurl; } function getName(path) { const File = java.io.File; @@ -155,27 +151,6 @@ function imageCompress(imgurl,fileid) { let f = fetch(imgurl, { inputStream: true }); - let size; - let info = getPicInfo(f); - 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; + let newpath = "/storage/emulated/0/Android/data/com.example.hikerview/files/Documents/_cache/"+(fileid||"")+"_"+getName(imgurl); + return compress(f, newpath); }