我们正在构建一个应用程序,它从库中选择一个图像,在imageView中显示它。当按下加密按钮时,它被传递给java类,在那里它被加密并存储在内存中。我想在加密前压缩图像。我正在使用Bitmap.scaledimage,但我不确切知道在哪里添加它
挑选图片的代码:
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 :(得分: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;
}
可能是这样的。我可以看到你的完整编码吗?我需要你的加密和解密部分。