图片采样率设置

package com.gyarmy.imageTest;

import java.io.IOException;

import android.media.ExifInterface;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {
	
	public ImageView iv;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        iv =(ImageView) findViewById(R.id.iv);
    }

    
    
    public void testBtn(View v){
    	
    	String path = Environment.getExternalStorageDirectory().toString();
    	path = path+"/Download/04.jpg";
    	System.out.println(path);
    	BitmapFactory.Options opts = new Options();
    	
    	//设置采样率
    	//屏幕的值
    	DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    	//宽和高的信息
    	int widthPixels = displayMetrics.widthPixels;
    	int heightPixels = displayMetrics.heightPixels;
    	
    	
    	//图片信息
    	try {
			ExifInterface exif = new ExifInterface(path);
			int imgWidth = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
			int imgHeight = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
			
			System.out.println(imgWidth +"===="+imgHeight);
			
			int sampleWidth = (int) (imgWidth*1f/widthPixels+0.5f);
			int sampleHeight = (int) (imgHeight*1f/heightPixels+0.5f);
			
			System.out.println(sampleWidth +"===="+sampleHeight);
			
			
			int sampleS = (int) (Math.sqrt(sampleWidth*sampleWidth + sampleHeight*sampleHeight)+0.5f);
			
			System.out.println(sampleS);
			
			opts.inSampleSize  = sampleS;
			
			//设置图片
			Bitmap decodeImg = BitmapFactory.decodeFile(path, opts);
			
			//加载图
			iv.setImageBitmap(decodeImg);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    
}

原文链接: 图片采样率设置 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-143.html )

发表评论

0则评论给“图片采样率设置”