The Mobile Galaxy The New Mobile Era Has Come | Mobile – A Time To Remember

26Jun/130

How to Fix Eclipse freezes on startup on the Mac

Eclipse Juno with SVN plugins on the Mac freezes on start up

Bug:

"Deadlock when JNA is loading" problem on Mac OS X [using SVN Kit connector]

Fixing:

This is some AWT related issue on the Mac. Starting Eclipse with -Djava.awt.headless=true solves this deadlock.

 

Share
Tagged as: , No Comments
19Dec/120

How to convert BufferedImage to byte[] in Java?

Method 1 (no cached):

BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.setColor(Color.WHITE);
        g.setBackground(Color.WHITE);
        g.clearRect(0, 0, 1, 1);
        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f);
        g.setComposite(ac);
        g.dispose();

        // Kevin 12/18/2012 change for no cached image to bytes[]
        try {

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            JPEGCodec.createJPEGEncoder(baos).encode(image);
            resultImageAsRawBytes = baos.toByteArray(); 
            sLogger.debug("[D] resultImageAsRawBytes is done!!!");

        } catch (Exception e) {
            sLogger.error("[E] resultImageAsRawBytes throw error!!!", e);

        }

Method 2(cached):
        BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.setColor(Color.WHITE);
        g.setBackground(Color.WHITE);
        g.clearRect(0, 0, 1, 1);
        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f);
        g.setComposite(ac);
        g.dispose();

        // open
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
	        // write
	        ImageIO.write(image, "gif", baos);
        } catch (IOException ioe) {
            sLogger.error("[I] Image write failed!!!", ioe);

        }
        // flush
        baos.flush();

        resultImageAsRawBytes = baos.toByteArray();

        // close
        baos.close();

For method 2, sometimes will throw exception in some cases because temporary folder.
Share
Tagged as: No Comments
1Nov/120

Convert \uXXXX code to UTF-8 readable Text

<code>native2ascii -encoding UTF-8 -reverse src.txt dest.txt</code>

Detailed description is here: http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/native2ascii.html

Share
Tagged as: No Comments
15May/120

How to change uppercase sentences to mixed text in Java?

/**
 *
 */
package com.go2log.kevin;
import java.text.BreakIterator;
import java.util.Locale;
/**
 * @author Kevin Sun
 *
 */
public class FormatParagraph {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String paragraph =
                "LINE BOUNDARY ANALYSIS DETERMINES WHERE A TEXT " +
                "STRING CAN BE BROKEN WHEN LINE-WRAPPING. THE " +
                "MECHANISM CORRECTLY HANDLES PUNCTUATION AND " +
                "HYPHENATED WORDS. ACTUAL LINE BREAKING NEEDS TO " +
                "ALSO CONSIDER THE AVAILABLE LINE WIDTH AND IS " +
                "HANDLED BY HIGHER-LEVEL SOFTWARE. ";

        BreakIterator iterator =
                BreakIterator.getSentenceInstance();

        String sentences = paragraphformatter(iterator, paragraph.toUpperCase());
        System.out.println("New Sentences: " + sentences);
    }

    private static String paragraphformatter(BreakIterator bi, String source) {
        StringBuffer formattedString = new StringBuffer();
        bi.setText(source);

        int lastIndex = bi.first();
        while (lastIndex != BreakIterator.DONE) {
            int firstIndex = lastIndex;
            lastIndex = bi.next();

            if (lastIndex != BreakIterator.DONE) {
                String sentence = source.substring(firstIndex, lastIndex);
                System.out.println("sentence = " + sentence);
                formattedString.append(toProperCase(sentence.toLowerCase()));

            }
        }
        return formattedString.toString();
    }

    private static String toProperCase(String name) {
        return name.substring(0, 1).toUpperCase() +
                   name.substring(1).toLowerCase();
    }
}

Result:
sentence = LINE BOUNDARY ANALYSIS DETERMINES WHERE A TEXT STRING CAN BE BROKEN WHEN LINE-WRAPPING.
sentence = THE MECHANISM CORRECTLY HANDLES PUNCTUATION AND HYPHENATED WORDS.
sentence = ACTUAL LINE BREAKING NEEDS TO ALSO CONSIDER THE AVAILABLE LINE WIDTH AND IS HANDLED BY HIGHER-LEVEL SOFTWARE.
New Sentences: Line boundary analysis determines where a text string can be broken when line-wrapping. The mechanism correctly handles punctuation and hyphenated words. Actual line breaking needs to also consider the available line width and is handled by higher-level software.
Share
Tagged as: , No Comments