如以下代码所示,我正在捕获图像,对其进行裁剪并使用它们。在裁剪图像时,图像严重失真并且没有质量。请帮助我。
public class ScanActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
captureImage();
}
private void captureImage() {
try {
//Capture the image from rear Camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//Crop the captured image
cameraIntent.putExtra("crop", "true");
cameraIntent.putExtra("aspectX", 0);
cameraIntent.putExtra("aspectY", 0);
cameraIntent.putExtra("outputX", 200);
cameraIntent.putExtra("outputY", 200);
startActivityForResult(cameraIntent, CAMERA_CAPTURE);
} catch (Exception e) {
Toast.makeText(ScanActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK || requestCode != CAMERA_CAPTURE)
return;
if (requestCode == CAMERA_CAPTURE) {
Bundle extras = data.getExtras();
if (extras != null) {
if (data != null) {
Bitmap bitmap = extras.getParcelable("data");
imageView.setImageBitmap(bitmap);
// convert bitmap to JPEG to save as a byte[]
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// compress the image
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
// convert to byte array
final byte[] imageInByte = stream.toByteArray();
}
我尝试了以下代码,但似乎没有任何帮助:
int newWidth=200;
int newHeight=200;
Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
float ratioX = newWidth / (float) bitmap.getWidth();
float ratioY = newHeight / (float) bitmap.getHeight();
float middleX = newWidth / 2.0f;
float middleY = newHeight / 2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
imageView.setImageBitmap(scaledBitmap);