设置 android 中捕获的图像的尺寸
Set the dimensions of image captured in android
在我的应用程序中,我有一个 activity,我们可以在其中使用 Intent 在单击按钮时从相机捕获图像,并将捕获的图像设置到图像视图中。它的工作绝对没问题。
但现在我想要特定尺寸的图像。例如,在我的例子中,我的图像视图的尺寸是 150dp * 150dp。所以我希望相机拍摄的图像尺寸也应始终为 150dp * 150dp,以便完美贴合。我该怎么做?
这是我的主要内容 activity。
public class MainActivity extends Activity implements OnClickListener {
Button btnTackPic;
TextView tvHasCamera, tvHasCameraApp;
ImageView ivThumbnailPhoto;
Bitmap bitMap;
static int TAKE_PICTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get reference to views
tvHasCamera = (TextView) findViewById(R.id.tvHasCamera);
tvHasCameraApp = (TextView) findViewById(R.id.tvHasCameraApp);
btnTackPic = (Button) findViewById(R.id.btnTakePic);
ivThumbnailPhoto = (ImageView) findViewById(R.id.ivThumbnailPhoto);
// Does your device have a camera?
if(hasCamera()){
tvHasCamera.setBackgroundColor(0xFF00CC00);
tvHasCamera.setText("You have Camera");
}
// Do you have Camera Apps?
if(hasDefualtCameraApp(MediaStore.ACTION_IMAGE_CAPTURE)){
tvHasCameraApp.setBackgroundColor(0xFF00CC00);
tvHasCameraApp.setText("You have Camera Apps");
}
// add onclick listener to the button
btnTackPic.setOnClickListener(this);
}
// on button "btnTackPic" is clicked
@Override
public void onClick(View view) {
// create intent with ACTION_IMAGE_CAPTURE action
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Edit to save picture
File file = new File(Environment.getExternalStorageDirectory(), "my-photo.jpg");
Uri photoPath = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);
// start camera activity
startActivityForResult(intent, TAKE_PICTURE);
}
// The Android Camera application encodes the photo in the return Intent delivered to onActivityResult()
// as a small Bitmap in the extras, under the key "data"
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent != null){
// get bundle
Bundle extras = intent.getExtras();
// get
bitMap = (Bitmap) extras.get("data");
ivThumbnailPhoto.setImageBitmap(bitMap);
}
}
// method to check you have a Camera
private boolean hasCamera(){
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
// method to check you have Camera Apps
private boolean hasDefualtCameraApp(String action){
final PackageManager packageManager = getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
}
提前致谢。
您需要像下面的代码一样缩放您的位图:
Bitmap originalBitmap = <the original bitmap>;
Bitmap resizedBitmap = Bitmap.createScaledBitmap(
originalBitmap, newWidth, newHeight, false);
因此根据您的要求,调整后的位图必须生成为:
Bitmap resizedBitmap = Bitmap.createScaledBitmap(
bitMap, 150,150, false);
注意:这里的150是以像素为单位的,所以你要计算150dp到像素然后再放置。
在我的应用程序中,我有一个 activity,我们可以在其中使用 Intent 在单击按钮时从相机捕获图像,并将捕获的图像设置到图像视图中。它的工作绝对没问题。
但现在我想要特定尺寸的图像。例如,在我的例子中,我的图像视图的尺寸是 150dp * 150dp。所以我希望相机拍摄的图像尺寸也应始终为 150dp * 150dp,以便完美贴合。我该怎么做?
这是我的主要内容 activity。
public class MainActivity extends Activity implements OnClickListener {
Button btnTackPic;
TextView tvHasCamera, tvHasCameraApp;
ImageView ivThumbnailPhoto;
Bitmap bitMap;
static int TAKE_PICTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get reference to views
tvHasCamera = (TextView) findViewById(R.id.tvHasCamera);
tvHasCameraApp = (TextView) findViewById(R.id.tvHasCameraApp);
btnTackPic = (Button) findViewById(R.id.btnTakePic);
ivThumbnailPhoto = (ImageView) findViewById(R.id.ivThumbnailPhoto);
// Does your device have a camera?
if(hasCamera()){
tvHasCamera.setBackgroundColor(0xFF00CC00);
tvHasCamera.setText("You have Camera");
}
// Do you have Camera Apps?
if(hasDefualtCameraApp(MediaStore.ACTION_IMAGE_CAPTURE)){
tvHasCameraApp.setBackgroundColor(0xFF00CC00);
tvHasCameraApp.setText("You have Camera Apps");
}
// add onclick listener to the button
btnTackPic.setOnClickListener(this);
}
// on button "btnTackPic" is clicked
@Override
public void onClick(View view) {
// create intent with ACTION_IMAGE_CAPTURE action
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Edit to save picture
File file = new File(Environment.getExternalStorageDirectory(), "my-photo.jpg");
Uri photoPath = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);
// start camera activity
startActivityForResult(intent, TAKE_PICTURE);
}
// The Android Camera application encodes the photo in the return Intent delivered to onActivityResult()
// as a small Bitmap in the extras, under the key "data"
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent != null){
// get bundle
Bundle extras = intent.getExtras();
// get
bitMap = (Bitmap) extras.get("data");
ivThumbnailPhoto.setImageBitmap(bitMap);
}
}
// method to check you have a Camera
private boolean hasCamera(){
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
// method to check you have Camera Apps
private boolean hasDefualtCameraApp(String action){
final PackageManager packageManager = getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
}
提前致谢。
您需要像下面的代码一样缩放您的位图:
Bitmap originalBitmap = <the original bitmap>;
Bitmap resizedBitmap = Bitmap.createScaledBitmap(
originalBitmap, newWidth, newHeight, false);
因此根据您的要求,调整后的位图必须生成为:
Bitmap resizedBitmap = Bitmap.createScaledBitmap(
bitMap, 150,150, false);
注意:这里的150是以像素为单位的,所以你要计算150dp到像素然后再放置。