Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

WriteFile don't work as expected #25

Closed
eduardovalentim opened this issue May 20, 2013 · 0 comments
Closed

WriteFile don't work as expected #25

eduardovalentim opened this issue May 20, 2013 · 0 comments
Assignees
Labels

Comments

@eduardovalentim
Copy link

Hi all,

I'm tring to create a OCR program using your code, but I'm having a lot of unexpected trouble and decided to create a JUnit test file to validate the class com.googlecode.leptonica.android.WriteFile.

The result of the tests follow below:

. The two writeBytes8 methods creates a invalid file format with 4.9Mb. I tried to open the file using Gimp 2.6 (attached image) but received a error dialog;
. The method writeFiles throw a RuntimeException with the message "writeFiles() is not currently supported". It is ok to me, because if the API say do not use, is supposed to don't use;
. The method writeMem always return null;
. The two methods writeImpliedFormat return true - that means success - but the files don't exist anywhere;
. The only method that works is the writeBitmap method.

The image used for test was taken from the camera and is a valid image.

Environment ---------------------------------------------------
Darwin Eduardo-Mac 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64

Eclipse Android Developer Tools: Build: v21.1.0-569685
Android Debug Bridge version 1.0.31
ndk-build version: GNU Make 3.81 (This program built for i386-apple-darwin10.8.0)

JUnit ---------------------------------------------------
package com.googlecode.leptonica.android.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import junit.framework.Assert;
import junit.framework.TestCase;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory.Options;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

import com.googlecode.leptonica.android.Constants;
import com.googlecode.leptonica.android.Pix;
import com.googlecode.leptonica.android.Pixa;
import com.googlecode.leptonica.android.ReadFile;
import com.googlecode.leptonica.android.WriteFile;

public class WriteFileFullTest extends TestCase {

private static final String WRITE_FILES = "WriteFiles.jpg";
private static final String WRITE_BYTES8 = "WriteBytes8Pix.jpg";
private static final String WRITE_BYTE_ARRAY = "WriteBytes8PixByteArray.jpg";
private static final String WRITE_MEM = "WriteMem.jpg";
private static final String WRITE_IMPLIED_FORMAT2 = "WriteImpliedFormat.jpg";
private static final String WRITE_IMPLIED_FORMAT = "WriteImpliedFormat2.jpg";
private static final String WRITE_BITMAP_JPG = "WriteBitmap.jpg";

private Bitmap bitmap;
private File dir;

public WriteFileFullTest() {
}

@Override
protected void setUp() throws Exception {
    this.dir = Environment.getExternalStorageDirectory();

    Log.i("WriteFileFullTest", "getExternalStorageDirectory: " + dir.getAbsolutePath());

    String pathName = dir.getAbsolutePath() + "/image.jpg";
    Log.i("WriteFileFullTest", "Bitmap pathName: " + pathName);

    Options options = new Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    bitmap = BitmapFactory.decodeFile( pathName, options);
}

@Override
protected void tearDown() throws Exception {
    bitmap.recycle();
}

public void testWriteBytes8Pix() throws IOException {
    Pix pixs = ReadFile.readBitmap(bitmap);
    try {
        byte[] buffer = WriteFile.writeBytes8(pixs);

        FileOutputStream out = new FileOutputStream( new File( dir, WRITE_BYTES8) );
        try {
            out.write(buffer);
        } finally {
            out.close();
        }
    } finally {
        pixs.recycle();
    }
}

public void testWriteBytes8PixByteArray() throws IOException {
    Pix pixs = ReadFile.readBitmap(bitmap);
    try {
        int size = pixs.getWidth() * pixs.getHeight();
        byte[] buffer = new byte[size];
        int written = WriteFile.writeBytes8(pixs, buffer);

        Assert.assertEquals(size, written);

        FileOutputStream out = new FileOutputStream( new File( dir, WRITE_BYTE_ARRAY) );
        try {
            out.write(buffer);
        } finally {
            out.close();
        }
    } finally {
        pixs.recycle();
    }
}

public void testWriteFiles() {
    Pix pixs = ReadFile.readBitmap(bitmap);
    try {
        Pixa pixa = Pixa.createPixa(pixs.getWidth() * pixs.getHeight(), pixs.getWidth(), pixs.getHeight());
        pixa.addPix(pixs, Constants.L_CLONE);

        boolean success = WriteFile.writeFiles(pixa, new File(dir, WRITE_FILES), "pre", Constants.IFF_JP2);
        if (!success) {
            fail("testWriteFiles fail!");
        }
    } finally {
        pixs.recycle();
    }
}

public void testWriteMem() throws IOException {
    Pix pixs = ReadFile.readBitmap(bitmap);
    try {
        // Constants.IFF_JP2 returns null
        // Constants.IFF_JFIF_JPEG returns null
        // Constants.IFF_PNG returns null
        byte[] buffer = WriteFile.writeMem(pixs, Constants.IFF_GIF);
        if (buffer == null) {
            fail("testWriteMem fail!");
        }

        FileOutputStream out = new FileOutputStream( new File( dir, WRITE_MEM) );
        try {
            out.write(buffer);
        } finally {
            out.close();
        }
    } finally {
        pixs.recycle();
    }
}

public void testWriteImpliedFormatPixFile() {
    Pix pixs = ReadFile.readBitmap(bitmap);
    try {
        boolean success = WriteFile.writeImpliedFormat(pixs, new File(dir, WRITE_IMPLIED_FORMAT2));
        if (!success) {
            fail("testWriteImpliedFormatPixFile fail!!");
        }
    } finally {
        pixs.recycle();
    }
}

public void testWriteImpliedFormatPixFileIntBoolean() {
    Pix pixs = ReadFile.readBitmap(bitmap);
    try {
        boolean success = WriteFile.writeImpliedFormat(pixs, new File(dir, WRITE_IMPLIED_FORMAT), 100, true);
        if (!success) {
            fail("testWriteImpliedFormatPixFile fail!!");
        }
    } finally {
        pixs.recycle();
    }
}

public void testWriteBitmap() throws IOException {
    Pix pixs = ReadFile.readBitmap(bitmap);
    try {
        Bitmap bmp = WriteFile.writeBitmap(pixs);
        FileOutputStream out = new FileOutputStream( new File( dir, WRITE_BITMAP_JPG) );
        try {
            bmp.compress(CompressFormat.JPEG, 100, out);
        } finally {
            out.close();
        }
    } finally {
        pixs.recycle();
    }
}

}

Thanks in advance for any help!!
screen shot 2013-05-20 at 12 00 41 am

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants