captured photo have grey line at bottom and size of image is very small in codenameone

时间:2018-08-22 14:00:13

标签: java codenameone

I am capturing an image, and I am storing it in my file system. Whereas I preview the photo by reading the byte stream.

Somewhere its not reading the byte data completely due to which I get the dark line at bottom of image. How I can use readfully() or readAll() here. When I tried using readall() method, the half of image didnt load properly. So, I am confused how to use this. I cant use File URI here as my image is having unique ID. Its working fine on simulator, but having issue on device. Any help on this would be really appreciated like how to use readfully() or readAll() method if that's the only solution.

public void saveFromFile(final String file, final boolean launchPreviewIfAnnotateAllowed) {

        FileSystemStorage fss = FileSystemStorage.getInstance();
        InputStream is = fss.openInputStream(file);


            final byte[] bytes = getBytes(is);
            is.close();
            is = null;


            model.updateMediaData(bytes);


            Image im = Image.createImage(bytes, 0, bytes.length);


}

private static byte[] getBytes(InputStream is) throws IOException {

    int len;
    int size = 1024;
    byte[] buf;

    if (is instanceof ByteArrayInputStream) {
        size = is.available();
        buf = new byte[size];
        len = is.read(buf, 0, size);
    } else {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        buf = new byte[size];
        while ((len = is.read(buf, 0, size)) != -1)
            bos.write(buf, 0, len);
        buf = bos.toByteArray();
    }
    return buf;
}

the updateMediaData() looks something like this

public void updateMediaData(byte[] binaryData) {
    updateMediaData(binaryData, null);
}

public void updateMediaData(final byte[] binaryData, final String filename) {

    mcHelper.updateMediaData(binaryData, photoId, filename);

}

  public void updateMediaData(byte[] binaryData, String componentId, String filename) {



    if (binaryData != null) {

        String userName = pj;
        userName = Strings.replaceAll(userName, " ", "_");  //USERNAME RENAME
        //now strip any bizzare chars from it
        String altered = "";
        for (int i = 0; i < userName.length(); i++) {
            char c = userName.charAt(i);
            if (c == '_' || Character.isDigit(c) || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                altered += c;
            }
        }

        userName = altered;

        final String muid = userName + DateTime.getMediaFileTimeStamp() + "_" + new Guid() + "_" + componentId 
                + "." + getExtension(binaryData);
        mediaData = new MediaData(muid);
        mediaData.setFormFieldId(owner.getIdInt());
        mediaData.setMediaType(MediaData.MEDIA_TYPE_PHOTO);
        if (muid != null && muid.indexOf(".jpg") != -1) {
            mediaData.setFilename(muid); 
        }
        else {
            mediaData.setFilename(filename != null ? filename : muid); // If filename isn't specified, use MUID. 
        }
        mediaData.setRevisionNumber(0);
        mediaData.setData(binaryData);
        mediaData.setMimeType(getMimeType(binaryData));
        if (mediaData.getMimeType().indexOf("octet-stream") != -1) {
            mediaData.setMimeType("image/jpeg"); //can happen when encrytion used
        }
        // Save the data to persistent storage.
        try {
            mm.saveData(mediaData);
        } catch (DataAccessException e) {
            Application.log(Application.LOG_LEVEL_WARNING, "Failed to save media data for MediaComponent.", e);
        }
    }


}

1 个答案:

答案 0 :(得分:1)

getBytes替换为:

private static byte[] getBytes(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Util.copy(is, bos);
    return bos.bos.toByteArray();
}

这将正确执行流复制。