«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

올해는 머신러닝이다.

epub reader source for android or java 본문

Android/Tip&Tech

epub reader source for android or java

행복한 수지아빠 2011. 7. 20. 19:18

link from : http://www.siegmann.nl/epublib/android

Android

Epublib-core runs on Android.
Reading, writing and manipulating epub files is fully supported.
Requirements

Getting started

  1. Download epublib-core-latest.jar from https://github.com/downloads/psiegman/epublib/epublib-core-latest.jar
  2. Download slf4j-android
  3. Add both to your android project


Example code

This Logs info of 'assets/books/testbook.epub'.
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
  
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.TOCReference;
import nl.siegmann.epublib.epub.EpubReader;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
  
/**
 * Log the info of 'assets/books/testbook.epub'.
 *
 * @author paul.siegmann
 *
 */
public class LogTestBookInfo extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    AssetManager assetManager = getAssets();
    try {
      // find InputStream for book
      InputStream epubInputStream = assetManager
          .open("books/testbook.epub");
  
      // Load Book from inputStream
      Book book = (new EpubReader()).readEpub(epubInputStream);
  
      // Log the book's authors
      Log.i("epublib", "author(s): " + book.getMetadata().getAuthors());
  
      // Log the book's title
      Log.i("epublib", "title: " + book.getTitle());
  
      // Log the book's coverimage property
      Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage()
          .getInputStream());
      Log.i("epublib", "Coverimage is " + coverImage.getWidth() + " by "
          + coverImage.getHeight() + " pixels");
  
      // Log the tale of contents
      logTableOfContents(book.getTableOfContents().getTocReferences(), 0);
    } catch (IOException e) {
      Log.e("epublib", e.getMessage());
    }
  }
  
  /**
   * Recursively Log the Table of Contents
   *
   * @param tocReferences
   * @param depth
   */
  private void logTableOfContents(List<TOCReference> tocReferences, int depth) {
    if (tocReferences == null) {
      return;
    }
    for (TOCReference tocReference : tocReferences) {
      StringBuilder tocString = new StringBuilder();
      for (int i = 0; i < depth; i++) {
        tocString.append("\t");
      }
      tocString.append(tocReference.getTitle());
      Log.i("epublib", tocString.toString());
  
      logTableOfContents(tocReference.getChildren(), depth + 1);
    }
  }
}