我想将我用相机拍摄的图像或从我的图像库中拍摄的图像放在布局中,但质量太低,正如您在这些照片中看到的那样:
原图:
1.-真实形象
2.-直接从相机拍摄这张照片
3.-从图片库中获取这张图片
第 2 和第 3 张图片的质量很差,特别是在第 3 张中,但我也想提高第 2 张的质量。有什么办法可以做到吗?
这是我用来拍照的代码(来自相机和图片库):
public class PhotoEditingActivity extends Activity {
private byte[] photoByteArray;
private Button btnRotate;
private ImageView ivPhoto;
private Bitmap bmp;
private Button btnEditDone;
private ChooseImageInputDialog dialogBuilder;
private AlertDialog dialog;
private Button btnCancel;
private Button btnChooseNewPhoto;
private String actualPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_editing);
setUpViews();
/*try{
getPhoto();
}catch(Exception e){
showOptionsDialog();
}*/
showOptionsDialog();
//first try to get photo from intent,
//if there is no photo
//show options to take one
}
private void showOptionsDialog() {
dialogBuilder = new ChooseImageInputDialog(this);
dialog = dialogBuilder.create();
dialog.show();
}
/*private void getPhoto() {
Intent i = this.getIntent();
Bundle extras = i.getExtras();
photoByteArray = extras.getByteArray("photo");
bmp = BitmapFactory.decodeByteArray(photoByteArray, 0, photoByteArray.length);
ivPhoto.setImageBitmap(bmp);
}*/
private void setUpViews() {
ivPhoto = (ImageView)findViewById(R.id.ivPhotoEditing);
btnRotate = (Button)findViewById(R.id.btnPhotoEditRotate90);
btnRotate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
bmp = Bitmap.createBitmap(bmp, 0, 0,
bmp.getWidth(), bmp.getHeight(),
matrix, true);
ivPhoto.setImageBitmap(bmp);
}
});
btnCancel = (Button)findViewById(R.id.btnPhotoEditingCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PhotoEditingActivity.this.setResult(RESULT_CANCELED);
PhotoEditingActivity.this.finish();
}
});
btnChooseNewPhoto =(Button)findViewById(R.id.btnPhotoEditingTakeNewPicture);
btnChooseNewPhoto.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showOptionsDialog();
}
});
btnEditDone = (Button)findViewById(R.id.btnPhotoEditDone);
btnEditDone.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if(photoByteArray!=null){
ByteArrayOutputStream os = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
photoByteArray = os.toByteArray();
Intent i = PhotoEditingActivity.this.getIntent();
BitmapData.setBitmap(bmp);
Log.d("actual path pea", actualPath);
PhotoEditingActivity.this.setResult(RESULT_OK, i);
PhotoEditingActivity.this.finish();
}else{
Toast t = Toast.makeText(PhotoEditingActivity.this, "Please take a picture first", Toast.LENGTH_SHORT);
t.show();
}
}
});
}
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
private Bitmap decodeFile(File f){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_SIZE=70;
//Find the correct scale value. It should be the power of 2.
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case ChooseImageInputDialog.ACTIVITY_SELECT_PHOTO:
if(resultCode == RESULT_OK){
Log.d("resultcode pea pick", "result ok");
Uri selectedImageUri=data.getData();
actualPath=getRealPathFromURI(selectedImageUri);
File file=new File(actualPath);
bmp=decodeFile(file); //this is new bitmap which you can use for your purpose
ByteArrayOutputStream os = new ByteArrayOutputStream();
//bmp.compress(Bitmap.CompressFormat.PNG, 65, os);
photoByteArray = os.toByteArray();
ivPhoto.setImageBitmap(bmp);
}else{
Log.d("resultcode pea pick", "result not ok");
}
break;
case ChooseImageInputDialog.ACTIVITY_CAMERA_PHOTO:
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
bmp = (Bitmap)extras.get("data");
ByteArrayOutputStream os = new ByteArrayOutputStream();
//bmp.compress(Bitmap.CompressFormat.PNG, 65, os);
photoByteArray = os.toByteArray();
ivPhoto.setImageBitmap(bmp);
actualPath = getRealPathFromURI(data.getData());
Log.d("take pic actual path", actualPath);
}
break;
}
}
}
将其放入布局的代码:
private void setUpViews() {
// TODO Auto-generated method stub
upSize = up.length;
upCount = 1;
downSize = down.length;
downCount = 1;
head = (ImageView)findViewById(R.id.galleryUp);
tShirt = (ImageView)findViewById(R.id.galleryDown);
rowUp1 = (ImageView)findViewById(R.id.ivMonkeyRowUp1);
rowUp2 = (ImageView)findViewById(R.id.ivMonkeyRowUp2);
rowDown1 = (ImageView)findViewById(R.id.ivMonkeyRowDown1);
rowDown2 = (ImageView)findViewById(R.id.ivMonkeyRowDown2);
llTab = (LinearLayout)findViewById(R.id.llTab);
llTop = (LinearLayout)findViewById(R.id.llTop);
monkeyTab = (LinearLayout)findViewById(R.id.llTab1);
monkeyTab.setOnClickListener(this);
shareTab = (LinearLayout)findViewById(R.id.llTab3);
shareTab.setOnClickListener(this);
web = (ImageView)findViewById(R.id.imageWeb);
web.setOnClickListener(this);
ll = (LinearLayout) findViewById(R.id.llPhoto);
bmp = BitmapData.getBitmap();
d =(Drawable) new BitmapDrawable (getResources(), bmp);
ll.setBackgroundDrawable(d);
head2 = (ImageView)findViewById(R.id.galleryUp2);
tShirt2 = (ImageView)findViewById(R.id.galleryDown2);
rowUp12 = (ImageView)findViewById(R.id.ivMonkeyRowUp12);
rowUp22 = (ImageView)findViewById(R.id.ivMonkeyRowUp22);
rowDown12 = (ImageView)findViewById(R.id.ivMonkeyRowDown12);
rowDown22 = (ImageView)findViewById(R.id.ivMonkeyRowDown22);
llPhoto2 = (LinearLayout) findViewById(R.id.llPhoto2);
}