Android Opencv 3.2.0图像像素值在写入和读取后发生变化

时间:2017-09-27 01:06:11

标签: android opencv image-processing android-bitmap image-formats

写入和读取图像后像素值发生了变化。 我们通过以下方式保存(txt文件和jpg)来自相机的图像:

SaveToTXTFile(bytesArrayFromCamera, pathToTxtFile);
mOriginalImage = ByteArrayToBitmap(bytesArrayFromCamera);
SaveToJPGFile(mOriginalImage, pathToOcrJPG);

public static boolean SaveToTXTFile(byte[] image, String fileFullName){
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileFullName));
        bos.write(image);
        bos.flush();
        bos.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return true;
}

public static Bitmap ByteArrayToBitmap(byte[] bytes){
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inMutable = true;
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = null;
    try{
        bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, bitmapOptions);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return bitmap;
}

public static boolean SaveToJPGFile(Bitmap bmp, String path){
    FileOutputStream out = null;
    try {
        File file = new File(path);
        if(file.exists()){
            file.delete();
        }
        File folder = new File(file.getParent());
        if(!folder.exists())
        {
            Boolean isSuccess = folder.mkdirs();
            if(!isSuccess)
                throw new Exception("Can't create directory(ies)");
        }
        out = new FileOutputStream(path);

        // bmp is your Bitmap instance, PNG is a lossless format, the compression factor (100) is ignored
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}

然后我们读取txt文件,将其转换为mat然后转换为位图并保存为jpg文件:

mat = GetBillMat(pathToTxtFile);
SaveMatToJPGFile(mat, pathToOcr1JPG);

public static Mat GetBillMat(String billFullName) throws IOException {
    byte[] bytes = ImageTxtFile2ByteArray(billFullName);
    Mat mat = BytesToMat(bytes);
    return mat;
}

public static byte[] ImageTxtFile2ByteArray(String path) throws IOException {
    File file = new File(path);
    int size = (int) file.length();
    byte[] image = new byte[size];
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    bis.read(image);
    bis.close();
    return image;
}

public static Mat BytesToMat(byte[] bytes) {
    if (!OpenCVLoader.initDebug()) {
        // Handle initialization error
    }
        Mat jpegData = new Mat(1, bytes.length, CvType.CV_8UC1);
        jpegData.put(0, 0, bytes);

        Mat bgrMat = new Mat();
        Mat dstMat = new Mat();
        bgrMat = Imgcodecs.imdecode(jpegData, Imgcodecs.IMREAD_COLOR);

        Imgproc.cvtColor(bgrMat, dstMat, Imgproc.COLOR_BGR2RGBA, 4);
        return dstMat;
}

public static boolean SaveMatToJPGFile(Mat mat, String path){
    Bitmap bmp = Bitmap.createBitmap(mat.width(), mat.height(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(mat, bmp);
    try {
        return SaveToJPGFile(bmp, path);
    }
    catch (Exception e){
        return false;
    }
    finally {
        bmp.recycle();
    }
}

现在我们与Beyond Compare进行比较:

comparing ocr to ocr1. take a look at the left-down corner. An example for pixels changing

我尝试使用png并获得以下内容:

comparing png images

0 个答案:

没有答案