2015-04-02 42 views
0

我們正在構建一個應用程序,從圖庫中選取一個圖像,將其顯示在imageView中。當加密按鈕被按下時,它被傳遞給一個java類,它被加密並存儲在內存中。我想在加密之前壓縮圖像。我使用Bitmap.scaledimage,但我不知道究竟在何處添加它需要傳遞一個壓縮的圖像進行加密Android

代碼採摘圖像:

public void onClick(View v) 
    { 
     Intent i= new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(i,SELECTED_PICTURE); 
    } 
    }); 

代碼用於顯示圖像:

protected void onActivityResult(int requestCode,int resultCode,Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    switch(requestCode) 
    { 
    case SELECTED_PICTURE: 
     if(resultCode==RESULT_OK) 
     { 

      Uri uri= data.getData(); 
      String[]projection= {MediaStore.Images.Media.DATA};                 
      Cursor cursor= getContentResolver().query(uri, projection, null, null, null); 
      cursor.moveToFirst(); 
      int columnIndex= cursor.getColumnIndex(projection[0]); 
      filePath= cursor.getString(columnIndex); 
      cursor.close(); 
      iv.setImageBitmap(BitmapFactory.decodeFile(filePath)); 
      } 
     break; 

代碼加密:

one = (Button) findViewById(R.id.button2); 
    one.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      try{ 
       blowfish encryptFile = new blowfish("thisismypassword"); 
       //destpath=Environment.getExternalStorageDirectory().getAbsolutePath()+nameoffolder; 
       destpath= x.getPath()+nameoffolder; 
       //destpath="/storage/sdcard/test"; 
       encryptFile.encrypt(filePath,destpath); 
       Toast.makeText(Image.this, 
         "Done encrypting..yey..", 
         Toast.LENGTH_SHORT).show(); 

       }catch (Exception e) { 
        Toast.makeText(Image.this, e.getMessage(), 
          Toast.LENGTH_SHORT).show();System.out.println("sells"); 
      } 
     } 
    }); 

文件x聲明如下:

final File x=new File(android.os.Environment.getExternalStorageDirectory(),File.separator+"Image"); 
    x.mkdirs(); 

FilePath是字符串。

我想知道我可以在哪裏添加圖像壓縮(代碼的哪一部分)。圖像壓縮我們需要位圖,但是我一直在使用uri,文件和字符串進行存儲和顯示。

+0

您正在加密.jpg文件(在filePath中)。爲什麼要壓縮.jpg文件?這沒有帶來什麼。 – greenapps 2015-04-02 20:36:23

+0

我們只是想在存儲時減小圖像的大小。感謝回覆。還有解密圖像的解密功能。我還沒有貼出代碼。我們希望解密後的圖像採用壓縮格式,以便節省大小 – Sowmya 2015-04-03 01:44:20

+0

究竟是什麼問題。將代碼加載到Bitmap中然後更改分辨率(widthxheight)的代碼已在此網站上發佈多次。 '我們希望解密後的圖像是壓縮格式,以節省大小'。對不起,但我不明白你在說什麼。 – greenapps 2015-04-03 08:20:26

回答

1
public Bitmap decodeSampledBitmapFromUri(String uri, int reqWidth, int reqHeight) { 

    Bitmap bm = null; 

    try{ 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(uri)), null, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(uri)), null, options); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 
    } 

    return bm; 
} 

public int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
      inSampleSize = Math.round((float)height/(float)reqHeight); 
     } else { 
      inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 
    } 
    return inSampleSize; 
} 

可能是這樣的。我可以看看你的完整編碼嗎?我需要你的加密和解密部分。

+0

雅在最後以上述方式實現我的代碼。謝謝 – Sowmya 2015-06-20 05:26:30