我在通过相机在Android上捕获图像时遇到问题。我在下面显示我的Android应用程序的来源。
public class RegisterActivity extends AppCompatActivity {
private Kelolajson json;
private Button btnfoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
......
json = new KelolaJson();
btnfoto = (Button) findViewById(R.id.fotobtn);
btnfoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isDeviceSupportCamera()) {
f_foto =txthp.getText().toString() +".jpg";
fname= txthp.getText().toString();
captureImage();
}
else {
Toast.makeText(getApplicationContext(),"Your Mobile don't support camera",Toast.LENGTH_LONG).show();
}
}
});
......
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image display it in image view
String root = Environment.getExternalStorageDirectory().toString();
String path = root + "/Pictures/myapp";
gmbpath = fileUri.getPath().toString();
Bitmap tmp = BitmapFactory.decodeFile(gmbpath);
imgpreview.setImageBitmap(tmp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(),
"You has been canceled capture.",
Toast.LENGTH_SHORT).show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry, Can\'t get photo",
Toast.LENGTH_SHORT).show();
}
}
}
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type) {
//External sdcard location
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"myapp");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile = null;
if (type == MEDIA_TYPE_IMAGE) {
fname = "_" + timeStamp;
f_foto = tmp_hp + ".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + f_foto);
} else {
mediaFile= null;
}
return mediaFile;
}
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
}
此源代码在移动智能手机上运行良好。但是某些智能手机捕获照片的结果始终会改变方向。例如,我通过智能手机纵向拍摄图像,但是智能手机拍摄图像的结果是横向拍摄。同样,如果为横向,则结果更改为纵向。此结果不符合智能手机的方向。
答案 0 :(得分:0)
检查与从相机捕获的图像的URI
相关的方向。
try {
getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
Log.v(Common.TAG, "Exif orientation: " + orientation);
} catch (Exception e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true);
显然,三星手机设置了EXIF方向标签,而不是旋转单个像素。使用BitmapFactory
读取位图不支持该标签。我发现此问题的解决方案是在{中使用ExifInterface
{1}}的活动方法。
答案 1 :(得分:0)
尝试以下代码:
private static final int CAMERA_PIC_REQUEST = 1337;
Button btnn;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnn = (Button) findViewById(R.id.btn);
imageView = (ImageView) findViewById(R.id.Img);
sf = getSharedPreferences("photo", Context.MODE_PRIVATE);
String de = sf.getString("image", "");
byte[] c = Base64.decode(de, Base64.DEFAULT);
Bitmap bb = BitmapFactory.decodeByteArray(c, 0, c.length);
imageView.setImageBitmap(bb);
btnn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK && null != data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
byte[] b = bytes.toByteArray();
String img = Base64.encodeToString(b, Base64.DEFAULT);
SharedPreferences.Editor editor = sf.edit();
editor.putString("image", img);
editor.commit();
imageView.setImageBitmap(thumbnail);
}
}