裁剪图像不起作用 (Android)
Cropping image doesn't work (Android)
对不起我的英语。我试图裁剪图像。我使用您可以在 1 和标准 Android 方法中找到的库。但是对于某些设备,CropImage.class
无法打开,我不知道为什么会这样。
我的获取图像功能的代码
//method for get image from camera
public void getPhotoCamera() {
dialogBuilder.cancel();
final int REQUEST_CAMERA = 2;
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/test/";
newfile = new File(dir);
outputFileUri = Uri.fromFile(newfile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 2:
try{
Bitmap myBitmap = decodeUri(outputFileUri);
Bitmap orientation = ExifUtil.rotateBitmap(newfile.getAbsolutePath(), myBitmap);
//start crop image
startCrop(orientation);
}catch(Exception e) {
Log.e("Photo", e.toString());
}
break;
}
}
//method start crop
public void startCrop(Bitmap bm) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Crop.class);
in1.putExtra("image",byteArray);
startActivity(in1);
}
class crop
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p_crop);
if(getSupportActionBar() != null) getSupportActionBar().hide();
Intent intent = getIntent();
Bundle extra = intent.getExtras();
if(extra != null) {
try{
byte[] byteArray = getIntent().getByteArrayExtra("image");
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
} catch (Exception e) {
Log.e("error", e.toString());}
}
final CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setMinFrameSizeInDp(100);
cropImageView.setImageBitmap(bmp);
RelativeLayout cropButton = (RelativeLayout)findViewById(R.id.crop_button);
cropButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
cropImageView.getCroppedBitmap().compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
}
});
}
为什么要将 byteArray 传递给 Crop.class
?改用 URI。
试试下面的代码:
//method for get image from camera
public void getPhotoCamera() {
dialogBuilder.cancel();
final int REQUEST_CAMERA = 2;
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/test/";
newfile = new File(dir);
outputFileUri = Uri.fromFile(newfile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 2:
startCrop();
break;
}
}
//method start crop
public void startCrop() {
Intent in1 = new Intent(this, Crop.class);
in1.putExtra("image", outputFileUri);
startActivity(in1);
}
和class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p_crop);
if(getSupportActionBar() != null) getSupportActionBar().hide();
Bitmap bitmap = null;
try {
Intent intent = getIntent();
imageUri = intent.getParcelableExtra("image");
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (Exception e) {
e.printStackTrace();
}
if (bitmap != null) {
final CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setMinFrameSizeInDp(100);
cropImageView.setImageBitmap(bitmap);
}
}
对不起我的英语。我试图裁剪图像。我使用您可以在 1 和标准 Android 方法中找到的库。但是对于某些设备,CropImage.class
无法打开,我不知道为什么会这样。
我的获取图像功能的代码
//method for get image from camera
public void getPhotoCamera() {
dialogBuilder.cancel();
final int REQUEST_CAMERA = 2;
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/test/";
newfile = new File(dir);
outputFileUri = Uri.fromFile(newfile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 2:
try{
Bitmap myBitmap = decodeUri(outputFileUri);
Bitmap orientation = ExifUtil.rotateBitmap(newfile.getAbsolutePath(), myBitmap);
//start crop image
startCrop(orientation);
}catch(Exception e) {
Log.e("Photo", e.toString());
}
break;
}
}
//method start crop
public void startCrop(Bitmap bm) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Crop.class);
in1.putExtra("image",byteArray);
startActivity(in1);
}
class crop
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p_crop);
if(getSupportActionBar() != null) getSupportActionBar().hide();
Intent intent = getIntent();
Bundle extra = intent.getExtras();
if(extra != null) {
try{
byte[] byteArray = getIntent().getByteArrayExtra("image");
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
} catch (Exception e) {
Log.e("error", e.toString());}
}
final CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setMinFrameSizeInDp(100);
cropImageView.setImageBitmap(bmp);
RelativeLayout cropButton = (RelativeLayout)findViewById(R.id.crop_button);
cropButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
cropImageView.getCroppedBitmap().compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
}
});
}
为什么要将 byteArray 传递给 Crop.class
?改用 URI。
试试下面的代码:
//method for get image from camera
public void getPhotoCamera() {
dialogBuilder.cancel();
final int REQUEST_CAMERA = 2;
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/test/";
newfile = new File(dir);
outputFileUri = Uri.fromFile(newfile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 2:
startCrop();
break;
}
}
//method start crop
public void startCrop() {
Intent in1 = new Intent(this, Crop.class);
in1.putExtra("image", outputFileUri);
startActivity(in1);
}
和class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p_crop);
if(getSupportActionBar() != null) getSupportActionBar().hide();
Bitmap bitmap = null;
try {
Intent intent = getIntent();
imageUri = intent.getParcelableExtra("image");
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (Exception e) {
e.printStackTrace();
}
if (bitmap != null) {
final CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setMinFrameSizeInDp(100);
cropImageView.setImageBitmap(bitmap);
}
}