public class MainActivity extends Activity {
ImageView image;
Activity context;
Preview preview;
static Camera camera;
Button exitButton;
ImageView fotoButton;
LinearLayout progressLayout;
String path = "/sdcard/KutCamera/cache/images/";
public static int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
fotoButton = (ImageView) findViewById(R.id.imageView_foto);
exitButton = (Button) findViewById(R.id.button_exit);
image = (ImageView) findViewById(R.id.imageView_photo);
progressLayout = (LinearLayout) findViewById(R.id.progress_layout);
preview = new Preview(this,
(SurfaceView) findViewById(R.id.KutCameraFragment));
FrameLayout frame = (FrameLayout) findViewById(R.id.preview);
frame.addView(preview);
preview.setKeepScreenOn(true);
fotoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
takeFocusedPicture();
} catch (Exception e) {
}
exitButton.setClickable(false);
fotoButton.setClickable(false);
progressLayout.setVisibility(View.VISIBLE);
}
});
}
@Override
protected void onResume() {
super.onResume();
// TODO Auto-generated method stub
if (camera == null) {
camera = Camera.open();
camera.startPreview();
camera.setErrorCallback(new ErrorCallback() {
public void onError(int error, Camera mcamera) {
camera.release();
camera = Camera.open();
Log.d("Camera died", "error camera");
}
});
}
if (camera != null) {
if (Build.VERSION.SDK_INT >= 14)
setCameraDisplayOrientation(context,
CameraInfo.CAMERA_FACING_BACK, camera);
preview.setCamera(camera);
}
}
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
Camera.AutoFocusCallback mAutoFocusCallback = new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
try {
camera.takePicture(mShutterCallback, null, jpegCallback);
} catch (Exception e) {
}
}
};
Camera.ShutterCallback mShutterCallback = new ShutterCallback() {
@Override
public void onShutter() {
// TODO Auto-generated method stub
}
};
public void takeFocusedPicture() {
camera.autoFocus(mAutoFocusCallback);
}
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
Calendar c = Calendar.getInstance();
File videoDirectory = new File(path);
if (!videoDirectory.exists()) {
videoDirectory.mkdirs();
}
try {
// Write to SD Card
outStream = new FileOutputStream(path
+ c.getTime().getSeconds() + ".jpg");
outStream.write(data);
Toast.makeText(MainActivity.this, "Image:" + count, 1000)
.show();
count++;
outStream.close();
if (count == 3) {
finish();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Bitmap realImage;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
options.inPurgeable = true;
options.inInputShareable = true;
realImage = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
if (data != null) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int screenHeight = getResources().getDisplayMetrics().heightPixels;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// Notice that width and height are reversed
Bitmap scaled = Bitmap.createScaledBitmap(realImage,
screenHeight, screenWidth, true);
int w = scaled.getWidth();
int h = scaled.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
realImage = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx,
true);
} else {
Bitmap scaled = Bitmap.createScaledBitmap(realImage,
screenWidth, screenHeight, true);
realImage = scaled;
}
}
image.setImageBitmap(realImage);
fotoButton.setClickable(true);
camera.startPreview();
progressLayout.setVisibility(View.GONE);
exitButton.setClickable(true);
}
};
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码使用这个我从相机拍摄图片并保存在Sdcard给定的路径我从相机在肖像模式下拍摄图像工作正常并保存在Sd卡但是保存图像当我打开它的横向模式但我想保留它在肖像模式,但我正在尝试更改表面持有人类中的许多东西,甚至主要的活动类没有链接,如果在景观设置相机预览然后其保存图像保存在肖像模式,而我想要相机预览和保存图像在Sd卡都应该在纵向模式,请建议我如何做到这一点
我遇到了同样的问题,但使用这个代码对我来说很有效,尝试这个代码:
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap bitmapFinal=null;
bitmapFinal = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
bmp.recycle();
bmp=null;
FileOutputStream fos = new FileOutputStream(pictureFile);
if(imageFormat.equals("jpg")||imageFormat.equals("jpeg"))
{
bitmapFinal.compress(Bitmap.CompressFormat.JPEG, 100,fos);
}
else if(imageFormat.equals("png"))
{
bitmapFinal.compress(Bitmap.CompressFormat.PNG, 100,fos);
}
else
{
bitmapFinal.compress(Bitmap.CompressFormat.JPEG, 100,fos);
}
bitmapFinal.recycle();
bitmapFinal=null;
fos.flush();
fos.close();
fos=null;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1128 次 |
| 最近记录: |