Back to project page adr.
The source code is released under:
Copyright (c) 2014, blake kim All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Re...
If you think the Android project adr listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.kkh.imagetest; /*ww w.ja v a 2s . co m*/ import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.media.ExifInterface; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.ViewTreeObserver; import android.widget.ImageView; import android.widget.ImageView.ScaleType; public class MainActivity extends Activity { static final String tag = "main"; ImageView mImgView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e(tag, "onCreate..."); loadImgToStorage(); mImgView = (ImageView) findViewById(R.id.ivImg); ViewTreeObserver vto = mImgView.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { // iv.getViewTreeObserver().removeOnPreDrawListener(this); // finalHeight = iv.getMeasuredHeight(); // finalWidth = iv.getMeasuredWidth(); // tv.setText("Height: " + finalHeight + " Width: " + finalWidth); Log.d(tag, "on observer, width=" + mImgView.getWidth()); Bitmap bitmap = getDecodedImage(); // ?????? ????????? ????? ??? ????? ????? ?? ??. // FIT_CENTER? ?? ?? ??. mImgView.setScaleType(ScaleType.FIT_CENTER); mImgView.setImageBitmap(bitmap); return true; } }); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); } void loadImgToStorage() { try { InputStream inf = getAssets().open("pic_exam.jpg"); OutputStream outf = new BufferedOutputStream( new FileOutputStream(getApplicationContext().getFilesDir()+"temp_img") ); byte[] buf = new byte[4*1024]; for(;;) { int rdcnt = inf.read(buf); if(rdcnt<=0) break; outf.write(buf, 0, rdcnt); } inf.close(); outf.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.d(tag, "load ok..."); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } Bitmap getDecodedImage() { // ??, ????????? ???? ??? ????. String filename = getApplicationContext().getFilesDir()+"temp_img"; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // ==> ??? ???? ?? ?? ??? ?? ?? ??. BitmapFactory.decodeFile(filename, options); Log.d(tag, "picture real width=" + options.outWidth); Log.d(tag, "picture real height=" + options.outHeight); // ????? ??? ?????? ??? ??? ????. int rotation = ExifInterface.ORIENTATION_NORMAL; try { ExifInterface exif; exif = new ExifInterface(filename); rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); } catch (IOException e) { Log.e(tag, "### Error: fail exif..."); e.printStackTrace(); } Log.d(tag, "picture rotation = " + rotation); // ????? ????? ??????? ???? ?? ???????? ???? ??? ??????. int imgw, imgh; if (rotation == ExifInterface.ORIENTATION_ROTATE_90 || rotation == ExifInterface.ORIENTATION_ROTATE_270) { imgw = options.outHeight; imgh = options.outWidth; } else { imgw = options.outWidth; imgh = options.outHeight; } // sampling ????? ????? ?????? ???? ?? inSampleSize? ????. // inSampleSize ? 2??? ?? ??? ??? ? ??. // sampling ????? ?? ?? ???? ?? ?? ??? ????????????? ??? ?? ???? ???? ? ??. int sx; int sy; ; int w, h; w = mImgView.getWidth(); h = mImgView.getHeight(); for (sx = 1; imgw / sx > mImgView.getWidth(); sx *= 2) ; for (sy = 1; imgh / sy > mImgView.getHeight(); sy *= 2) ; int sample_size = sx > sy ? sx : sy; Log.d(tag, "each samplesize x= " + sx + ", y=" + sy + ", result sample size = " + sample_size); options.inSampleSize = sample_size; // ?? ??? ??. options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(filename, options); if (bitmap != null) { float deg; switch (rotation) { case ExifInterface.ORIENTATION_ROTATE_90: deg = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: deg = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: deg = 270; break; default: deg = 0; break; } Log.d(tag, "rotation degree = " + deg); // ??????? ?? ???? ???? ?? ????? ????? ?? ?? ?? ???? ??. Matrix matrix = new Matrix(); matrix.preRotate(deg); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); return bitmap; } else { Log.d(tag, "### fail: decoding error..."); return null; } } }