我正在创建一个为用户打开相机的应用程序,在捕获图像后,它将在ImageView上显示!但ImageView中的图像质量非常差 这是代码:
public class Camera extends AppCompatActivity implements View.OnClickListener {
ImageView imgView;
Button camera, setBackground;
Intent i;
int cameraData = 0;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
initialize();
}
private void initialize() {
imgView = (ImageView) findViewById(R.id.bPicture);
camera = (Button) findViewById(R.id.bOpenCamera);
setBackground = (Button) findViewById(R.id.bSetBackground);
camera.setOnClickListener(this);
setBackground.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
Bitmap resizedBmp = Bitmap.createScaledBitmap(bitmap, imgView.getWidth(),imgView.getHeight(), false);
imgView.setImageBitmap(resizedBmp);
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bOpenCamera:
i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
case R.id.bSetBackground:
try {
WallpaperManager wallmngr = WallpaperManager.getInstance(this);
wallmngr.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
我该怎么做才能提高图像质量?
答案 0 :(得分:2)
使用缩略图以外的内容。引用the documentation for ACTION_IMAGE_CAPTURE
,重点补充:
调用者可以传递额外的EXTRA_OUTPUT来控制该图像的写入位置。如果EXTRA_OUTPUT不存在,则在额外字段中返回小尺寸图像作为Bitmap对象。这对于只需要小图像的应用程序非常有用。如果存在EXTRA_OUTPUT,则将将全尺寸图像写入EXTRA_OUTPUT的Uri值。
因此,请在Uri
中指定应写入完整相机图像的EXTRA_OUTPUT
。然后,使用an image-loading library(例如Picasso)将照片加载到ImageView
。
Here is a sample app演示使用EXTRA_OUTPUT
。
答案 1 :(得分:1)
这就是我让它为我工作的方式!假设您正在捕获图像,并希望在新的活动中显示捕获的图像,您可以按照这种方式:
首先点击按钮,您可以:
public void cameraFuture(View view) // <-- onClick() method of Camera Button
{
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"MyPhoto.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent, TAKE_PIC);
}
然后在onActivityResult()
方法上,您可以这样做:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PIC && resultCode==RESULT_OK){
Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();
Intent bitIntent = new Intent(this, CameraTake.class);
bitIntent.putExtra("imageUri", outPutfileUri);
startActivity(bitIntent);
finish();
}
}
在下一个Activity中,您可以通过以下方式接收文件:
Intent receiveIntent = getIntent();
uri = receiveIntent.getParcelableExtra("imageUri");
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
receiveImg= null;
} else {
receiveImg= extras.getString("PASSER");
}
} else {
receiveImg= (String) savedInstanceState.getSerializable("PASSER");
}
File imgFile = new File(Environment.getExternalStorageDirectory(),"MyPhoto.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.camOut);
myImage.setImageBitmap(myBitmap);
}
希望它有所帮助!快乐的编码:)