android通過手機拍照或本地相冊上傳清晰的圖片


首先是調出相機拍照的功能,我是先將該圖片存儲在本地中

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 調用android自帶的照相機
                // f_c_image.jpg 表示發美麗的照相圖片。
                Uri imageUri = Uri.fromFile(new File(getImageCachePath(),
                        "f_c_image.jpg"));
                //
                // intent.putExtra("crop", "true"); // 出現裁剪窗口。
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                //
                startActivityForResult(intent, 0);

public String getImageCachePath()//給圖片一個存儲路徑
    {
        if (!isExistSDCard())
        {
            return "";
        }
        
        String sdRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
        String result = sdRoot +
                "/" + MAIN_SOFT_FOLDER_NAME + "/" + CACHE_FOLDER_NAME;
        if (new File(result).exists() && new File(result).isDirectory())
        {
            return result;
        }
        else {
            return sdRoot;
        }
    }
    

另一種則是在本地相冊中調出圖片

Uri imageUri = Uri.fromFile(new File(getImageCachePath(),
                        "f_c_image.jpg"));

                Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
                openAlbumIntent.setType("image/*");
                openAlbumIntent.putExtra("crop", "true");
                // openAlbumIntent.putExtra("aspectX", 1);
                // openAlbumIntent.putExtra("aspectY", 1);
                // openAlbumIntent.putExtra("outputX", 240);
                // openAlbumIntent.putExtra("outputY", 240);
                // openAlbumIntent.putExtra("return-data", true);

                // lixi 直接從圖庫選擇圖片。
                openAlbumIntent.putExtra("output", imageUri);
                openAlbumIntent.putExtra("outputFormat", "JPEG");

                startActivityForResult(openAlbumIntent, 1);

所有對取到的圖片的處理都是在android的下面這個方法中進行,現在已經將圖片保存於本地,下面方法的功能是將該圖片放在另外一個activity顯示

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {

            Intent intent = new Intent();
            intent.setClass(getActivity(), AddNewPictureActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("FromWhere", FromWhere);

            switch (requestCode) {

            case TAKE_PICTURE:
               
                ImageTools.ClearNewTempImages(this.getActivity());
                // 判斷圖片還在不在?
                String sdPath = FileUtil.getImageCachePath();
                if (!ImageTools.findPhotoFromSDCard(sdPath, "f_c_image")) {
                    //  拍照的圖片沒有保存到本地目錄下。
                    // TODO:這里要提示一個文言。
                    Toast.makeText(getActivity(), "拍照失敗!", Toast.LENGTH_SHORT)
                            .show();
                    
                    
                    //ImageTools.deleteLatestPhoto(this.getActivity());
                    return;

                }
                
                //ImageTools.deleteLatestPhoto(this.getActivity());
                // 會莫名其妙的啟動兩次?
                // 啟動圖片預覽和保存的界面。
                bundle.putInt("CurrentPage", TAKE_PICTURE);

                intent.putExtras(bundle);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);

             

                break;

            case CHOOSE_PICTURE:
                // Bitmap smallBitmap = (Bitmap) data.getExtras().get("data");
                if (!ShowToastUtil.showNoSDCToast(getActivity()))
                {
                    return;
                }

                
                String sdPath1 = FileUtil.getImageCachePath();
                if (!ImageTools.findPhotoFromSDCard(sdPath1, "f_c_image")) {
                    // 選擇的圖片沒有保存到本地目錄下。
                    // TODO:這里要提示一個文言。
                    Toast.makeText(getActivity(), "取圖失敗!", Toast.LENGTH_SHORT)
                            .show();
                    return;

                }

                bundle.putInt("CurrentPage", CHOOSE_PICTURE);
                intent.putExtras(bundle);

                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);

               
                break;

            default:
                break;
            }
        }
    }

 在另一個activity中獲得Bitmap格式的文件顯示,在文件中使用的destW和destH都是800,fixRotating為true(其實有點不明白destW和destH是干什么用的)

public static Bitmap decodePhotoFromSDCard(String path, String photoName, int destW, int destH, boolean fixRotating)
    {
        String imgpath = path + "/" +photoName +".jpg";
        InputStream is = null;
        InputStream is2 = null;
        try {
            is = new FileInputStream(imgpath);
            int heightRatio = 1;
            int widthRatio = 1;
            BitmapFactory.Options opts=new BitmapFactory.Options();
            // 先解析圖片的邊緣獲取它的尺寸。
            opts.inJustDecodeBounds = true;
            Bitmap temp =BitmapFactory.decodeStream(is,null, opts); 
            
            is.close();
            is = null;
            
            heightRatio = (int)Math.ceil(opts.outHeight/(float)destH);  
            widthRatio = (int)Math.ceil(opts.outWidth/(float)destW);
            
            temp = null;
            
            
            
            //2.為位圖設置100K的緩存
            opts.inTempStorage = new byte[100 * 1024];
            //3.設置位圖顏色顯示優化方式
            //ALPHA_8:每個像素占用1byte內存(8位)
            //ARGB_4444:每個像素占用2byte內存(16位)
            //ARGB_8888:每個像素占用4byte內存(32位)
            //RGB_565:每個像素占用2byte內存(16位)
            opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
            //4.設置圖片可以被回收,創建Bitmap用於存儲Pixel的內存空間在系統內存不足時可以被回收
            opts.inPurgeable = true;
            
            if (heightRatio > 1 && widthRatio > 1)  
            {  
                int tempRatio = heightRatio > widthRatio ? heightRatio:widthRatio; 
                opts.inSampleSize = tempRatio / 2 *2;  // 保證不出錯,使用2的倍數來。
            } 
            
            opts.inJustDecodeBounds = false;  
            
            //6.設置解碼位圖的尺寸信息
            opts.inInputShareable = true; 
            
            //7.解碼位圖
            is2 = new FileInputStream(imgpath);
            Bitmap result1 =BitmapFactory.decodeStream(is2,null, opts);    
            
            // 解決圖片拍照后旋轉的問題。
            if (fixRotating)
            {
                int degree = ImageTools.readPictureDegree(imgpath);
                Bitmap result = ImageTools.rotaingImageView(degree, result1);
                
                return result;
            }
            else
            {
                return result1;
            }
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return null;
        }
        finally
        {
            try {
                if (is != null)
                {
                    is.close();
                }
                if (is2 != null)
                {
                    is2.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        
    }

這是我寫的第一篇博客,寫的目的是希望自己能堅持將項目中遇到問題的解決方法記錄下來。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM