runtime.images.initOpenCvIfNeeded();
importClass(org.opencv.core.CvType);
importClass(org.opencv.core.Scalar);
importClass(org.opencv.core.Point);
importClass(java.util.LinkedList);
importClass(org.opencv.imgproc.Imgproc);
importClass(org.opencv.imgcodecs.Imgcodecs);
importClass(org.opencv.core.Core);
importClass(org.opencv.core.Mat);
importClass(org.opencv.core.MatOfDMatch);
importClass(org.opencv.core.MatOfKeyPoint);
importClass(org.opencv.core.MatOfRect);
importClass(org.opencv.core.Size);
importClass(org.opencv.features2d.DescriptorMatcher);
importClass(org.opencv.features2d.Features2d);
importClass(android.graphics.Matrix);
importClass(org.opencv.android.Utils);
importClass(android.graphics.Bitmap);
importPackage(org.opencv.highgui.Highgui);
importPackage(org.opencv.features2d);
importClass(java.io.File);
importClass(java.io.FileOutputStream);
function mat2bitmap(img) {
let width = img.width();
let height = img.height();
let bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Utils.matToBitmap(img, bitmap);
saveBitmap(bitmap,"/sdcard/de.png")
//bitmap = zoomBitmap(bitmap,0.5)
return bitmap;
}
function saveBitmap(bitmap,path) {
threads.start(function(){
try {
filePic = new File(path);
let fos = new FileOutputStream(filePic);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (e) {
console.log(e);
return;
}
console.log("tag", "saveBitmap success: ");
})
}
function zoomBitmap(bitmap, scale) {
let matrix = new Matrix();
matrix.postScale(scale, scale); //长和宽放大缩小的比例
let resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizeBmp;
}
public File saveBitmap(Bitmap bitmapUse) {
Log.e("wy", "開始保存");
//生成一個文件,存儲我們將來拍照的照片
String sdPath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/a1";
File file = new File(sdPath);
Log.e("wy","絕對文件路徑: "+ file.getAbsoluteFile());
Log.e("wy","文件名: "+ file.getName());
if (!file.exists()) {
file.mkdirs();
Log.e("wy", "創建文件夾,路徑:"+file.getPath());
}
String mPath = System.currentTimeMillis() + "";
Log.e("wy", "保存路徑: " + sdPath);
File f = new File(sdPath, mPath + ".jpg");
// File f = new File("/sdcard/namecard/", picName);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
// 10M
// bitmapUse.compress(Bitmap.CompressFormat.PNG, 90, out);
// 0.5M
bitmapUse.compress(Bitmap.CompressFormat.JPEG, 45, out);
out.flush();
out.close();
Log.e("wy", "已經保存");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}