다음 코드를 사용하여 사진을 찍을 수 있습니다. 캡쳐 버튼을
클릭 : onActivityResult를에서
private void cameraCapture() {
File photoFile = null;
if (pic_number >= 5) {
Toast.makeText(getApplicationContext(), "Maximum amount of pictures taken", Toast.LENGTH_LONG).show();
} else {
try {
photoFile = createImageFile();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "File not created", Toast.LENGTH_LONG).show();
}
if (photoFile != null) {
Intent camIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
camera_picture_uri = photoFile.toString();
camIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(camIntent, pic_number);
pic_number++;
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getApplicationContext().getExternalFilesDir(null);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
return image;
}
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
setCameraPicture(data);
}
private void setCameraPicture(Intent data) {
// TODO
// Auto-generated method stub
Bitmap bm = null;
try {
File file = new File(camera_picture_uri);
ExifInterface ei = new ExifInterface(camera_picture_uri);
int rotation = 0;
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
rotation = 90;
bm = rotateCameraImage(rotation, file);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
rotation = 180;
bm = rotateCameraImage(rotation, file);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
rotation = 270;
bm = rotateCameraImage(rotation, file);
} else {
bm = rotateCameraImage(rotation, file);
}
// Set bitmap to imageview
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
// TODO: handle exception
e.printStackTrace();
}
}
private Bitmap rotateCameraImage(int rotation, File f) throws FileNotFoundException {
// TODO Auto-generated method stub
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
BitmapFactory.Options o = new BitmapFactory.Options();
int scale = 8;
o.inSampleSize = scale;
o.inPurgeable = true;
o.inInputShareable = true;
Bitmap sourceBitmap = BitmapFactory.decodeFile(f.toString(), o);
Bitmap bitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);
System.gc();
return bitmap;
}
편집
ExIfInterface를 사용하는이 사용자 정의 ROM을뿐만 아니라로드 한 삼성 기기에서 작동 올바르게 표시하기 위해 이미지 회전 설정을 확인하려면
스택 추적을 게시하십시오. –
오류 로그를 게시하십시오. –
내 대답을 확인하십시오 – Lunchbox