android camera根据屏幕图像大小设置显示
16lz
2022-05-27
package com.example.camera;import java.io.File;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.text.format.Time;import android.util.Log;import android.view.Display;import android.widget.ImageView;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class MainActivity extends Activity { private final static String TAG="camera"; private final static int CAMERA_RESULT =0; private ImageView view; private String imageFilePath; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //得到当前系统时间 Time t=new Time(); t.setToNow(); int year=t.year; int month=t.month; int day=t.monthDay; int hour=t.hour; int minute=t.minute; int second=t.second; Log.i(TAG, ""+year+month+day+hour+minute+second); String filename=""+year+month+day+hour+minute+second; //得到SD卡的路径也设置文件名 //这里可以简化的写成imageFilePath=Uri.parse("file:////sdcard/my.jpg"); /*imageFilePath=Environment.getExternalStorageDirectory() .getAbsolutePath()+"/my01.jpg";*/ imageFilePath=Environment.getExternalStorageDirectory() .getAbsolutePath()+"/"+filename+".jpg"; //创建文件 File file=new File(imageFilePath); //格式化为Uri Uri fileImageFilePath=Uri.fromFile(file); view=(ImageView)findViewById(R.id.imageview); Intent i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);//启动intent //设置到意图中 i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileImageFilePath); startActivityForResult(i, CAMERA_RESULT); } //返回接收 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(resultCode==RESULT_OK){ //以1/8加载原图像的大小 /*BitmapFactory.Options options=new BitmapFactory.Options(); options.inSampleSize=8; Bitmap bitmap=BitmapFactory.decodeFile(imageFilePath, options);*/ /*Bundle bundle=data.getExtras(); Bitmap bitmap=(Bitmap) bundle.get("data");*/ // view.setImageBitmap(bitmap); //得到图像的大小和显示的大小,动态解压 Display display=getWindowManager().getDefaultDisplay(); int dw=display.getWidth(); int dh=display.getHeight(); //加载图像 BitmapFactory.Options options=new BitmapFactory.Options(); options.inJustDecodeBounds=true;//设置之后可以设置长宽 Bitmap bitmap=BitmapFactory.decodeFile(imageFilePath, options); int heightRatio=(int)Math.ceil(options.outHeight/(float)dh); int widthRatio=(int)Math.ceil(options.outWidth/(float)dw); Log.i(TAG, "heith:"+heightRatio); Log.i(TAG,"width:"+widthRatio); //判断长宽哪个大 if(heightRatio>1 && widthRatio>1){ if(heightRatio>widthRatio){ options.inSampleSize=heightRatio; }else{ options.inSampleSize=widthRatio; } } //对它进行真正的解码 options.inJustDecodeBounds=false; bitmap=BitmapFactory.decodeFile(imageFilePath, options); view.setImageBitmap(bitmap); } } }
更多相关文章
- android在java中动态改变控件大小
- Android必会小功能总结
- ListView多次调用getView方法
- Android判断app是否打开消息通知并跳转设置
- Android(安卓)在Activity中获取控件尺寸的方法
- 可循环显示图像的Android(安卓)Gallery组件
- android 4.0 browser useragent debug
- 可循环显示图像的Android(安卓)Gallery组件
- 可循环显示图像的Android(安卓)Gallery组件