Android Open Source - Mpo_File_Parser_Example Main Activity






From Project

Back to project page Mpo_File_Parser_Example.

License

The source code is released under:

MIT License

If you think the Android project Mpo_File_Parser_Example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*The MIT License (MIT)
*//from  www . java2s  .c  o m
*Copyright (c) 2014 Poming Chen
*
*Permission is hereby granted, free of charge, to any person obtaining a copy
*of this software and associated documentation files (the "Software"), to deal
*in the Software without restriction, including without limitation the rights
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all
*copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*SOFTWARE.
*/

package tw.edu.nccu.cs.vipl.fun.mpo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import tw.edu.nccu.cs.vipl.task.CancelableRunnable;
import tw.edu.nccu.cs.vipl.task.TaskManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MainActivity extends Activity implements SurfaceHolder.Callback {

    private SeekBar mSeekBar;
    private Point mScreenSize;
    private MpoFileParser mMpoFileParser;
    private String mFileName;

    private SurfaceHolder mSurfaceHolder;
    private Matrix mMatrix;
    private TextView mTextView;
    private SurfaceView mSurfaceView;
    private Paint paint = new Paint();
    private TaskManager mLoadBitmapTaskManager;

    private boolean isStartingLoadIngFile = false;
    private final static String SAMPLE_MPO_FILE_NAME = "sample.mpo";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.textView);
        mTextView.setText(R.string.mode_three);
        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        mSurfaceView.getHolder().addCallback(this);

        mMatrix = new Matrix();
        mSeekBar = (SeekBar) findViewById(R.id.seekBar);

        mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                if (mMpoFileParser == null || !mMpoFileParser.isLoaded()) {
                    return;
                }

                mLoadBitmapTaskManager.clearTask();
                LoadBitmapTask bitmapCallable = new LoadBitmapTask(progress);
                mLoadBitmapTaskManager.submit(bitmapCallable);

            }
        });
        mScreenSize = new Point();
        getWindowManager().getDefaultDisplay().getSize(mScreenSize);
        mLoadBitmapTaskManager = new TaskManager("loadBitmap");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private class LoadBitmapTask extends CancelableRunnable {
        private int mProgress = 0;

        public LoadBitmapTask(int progress) {
            this.mProgress = progress;
        }

        @Override
        public void doTask() {

            Bitmap bitmap = mMpoFileParser.getBitmap(mProgress);

            Canvas canvas = mSurfaceHolder.lockCanvas();
            if (canvas != null && bitmap != null && !bitmap.isRecycled()) {
                canvas.drawBitmap(bitmap, mMatrix, paint);
                bitmap.recycle();
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }

        }

    }

    private class InitMpoFileTask extends AsyncTask<Void, Void, Integer> {

        private final static int FILE_NOT_FIND_ERROR = -1;
        private final static int FILE_PARSE_FAILED = -2;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            isStartingLoadIngFile = true;
        }

        @Override
        protected Integer doInBackground(Void... params) {

            try {
                mMpoFileParser = new MpoFileParser(mFileName, true);
            } catch (FileNotFoundException e) {
                return FILE_NOT_FIND_ERROR;
            } catch (IOException e) {
                return FILE_PARSE_FAILED;
            }
            return mMpoFileParser.getNumberOfImages();
        }

        @Override
        protected void onPostExecute(Integer result) {
            if (result == FILE_NOT_FIND_ERROR) {
                Toast.makeText(MainActivity.this, "CAN'T FIND FILE", Toast.LENGTH_LONG).show();
                return;
            }
            if (result == FILE_PARSE_FAILED) {
                Toast.makeText(MainActivity.this, "PARSE FILE FAILED", Toast.LENGTH_LONG).show();
                return;
            }

            mSeekBar.setMax(result - 1);
            mSeekBar.setProgress(0);
            resetMatrix();
            mTextView.setText("Open File:" + mMpoFileParser.getSimpleName());
            mLoadBitmapTaskManager.clearTask();
            LoadBitmapTask bitmapCallable = new LoadBitmapTask(0);
            mLoadBitmapTaskManager.submit(bitmapCallable);
            super.onPostExecute(result);
        }
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (mMpoFileParser == null) {
            return;
        }
        mScreenSize.set(width, height);
        resetMatrix();
        mLoadBitmapTaskManager.clearTask();
        LoadBitmapTask bitmapCallable = new LoadBitmapTask(mSeekBar.getProgress());
        mLoadBitmapTaskManager.submit(bitmapCallable);

    }

    private void resetMatrix() {
        int orientation = mMpoFileParser.getOrientation();
        Point point = mMpoFileParser.getImageSize();
        float scale;
        if (orientation % 180 == 0) {
            scale = Math.min((float) mScreenSize.x / point.x, (float) mScreenSize.y / point.y);
        } else {
            scale = Math.min((float) mScreenSize.y / point.x, (float) mScreenSize.x / point.y);
        }
        mMatrix.reset();
        mMatrix.postTranslate((mScreenSize.x - point.x) / 2, (mScreenSize.y - point.y) / 2);
        mMatrix.postRotate(orientation, mScreenSize.x / 2, mScreenSize.y / 2);
        mMatrix.postScale(scale, scale, mScreenSize.x / 2, mScreenSize.y / 2);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mSurfaceHolder = holder;
        if (getIntent().getDataString() != null && !isStartingLoadIngFile) {
            mFileName = getIntent().getDataString().replace("file://", "");
        } else {
            File file = new File(getSampleFilePath());
            if (file.exists()) {
                mFileName = file.getAbsolutePath();
            } else {
                try {
                    mFileName = createFileFromInputStream(this.getAssets().open(SAMPLE_MPO_FILE_NAME));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        if (mFileName == null) {
            finish();
            return;
        }
        new InitMpoFileTask().execute();

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mSurfaceHolder = null;
    }

    private String getSampleFilePath() {
        if (this.getExternalCacheDir() != null) {
            return this.getExternalCacheDir().getPath() + "/" + SAMPLE_MPO_FILE_NAME;
        }
        return this.getCacheDir().getPath() + "/" + SAMPLE_MPO_FILE_NAME;
    }

    private String createFileFromInputStream(InputStream inputStream) {

        OutputStream outputStream = null;
        try {
            File f = new File(getSampleFilePath());
            outputStream = new FileOutputStream(f);
            byte buffer[] = new byte[2048];
            int length = 0;

            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
            return f.getAbsolutePath();
        } catch (IOException e) {

        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return "";
    }

}




Java Source Code List

tw.edu.nccu.cs.vipl.fun.mpo.FileChannelReader.java
tw.edu.nccu.cs.vipl.fun.mpo.MainActivity.java
tw.edu.nccu.cs.vipl.fun.mpo.MpoFileParser.java
tw.edu.nccu.cs.vipl.task.BlockingLinkedList.java
tw.edu.nccu.cs.vipl.task.CancelableRunnable.java
tw.edu.nccu.cs.vipl.task.TaskManager.java