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 camera根據屏幕圖像大小設置顯示
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.