android自定义相机开发,android自定义相机通用解决方案_android-camera_开发99编程知识库...

我用 Activity 實現了一張照片,你可以拍攝照片並設置照片的方向。 我測試的每個設備都包括 Samsung Galaxy 系列,平板電腦,索尼,系列,平板電腦。

你可以查看我關於這裡主題的圖像旋轉的接受答案:

android設備上的攝像機捕獲方向

我將該部分的照片設置為主中的imageview,以英鎊為單位:try {

File imageFile = new File(cursor.getString(0));

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("","Exif orientation:" + orientation);

} catch (Exception e) {

e.printStackTrace();

}

Matrix matrix = new Matrix();

matrix.postRotate(rotate);

bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);

testImage.setImageBitmap(null);

testImage.setImageBitmap(bmp);

相機 Activity 中的常量值:private static final int ORIENTATION_PORTRAIT_NORMAL = 1;

private static final int ORIENTATION_PORTRAIT_INVERTED = 2;

private static final int ORIENTATION_LANDSCAPE_NORMAL = 3;

private static final int ORIENTATION_LANDSCAPE_INVERTED = 4;

private OrientationEventListener mOrientationEventListener;

private int mOrientation = -1;

相機 Activity 中的回調函數:Camera.PictureCallback photoCallback=new Camera.PictureCallback(){

public void onPictureTaken(final byte[] data, final Camera camera){

dialog=ProgressDialog.show(CameraActivity.this,"","Please wait while the photo is being saved..");

new Thread(){

public void run(){

try{

Thread.sleep(1000);

}

catch(Exception ex){}

onPictureTake(data,camera);

}

}.start();

}

};

相機 Activity 中的照片功能為 :public void onPictureTake(byte[] data, Camera camera){

switch (mOrientation) {

case ORIENTATION_PORTRAIT_NORMAL:

rotate = 90;

break;

case ORIENTATION_LANDSCAPE_NORMAL:

rotate = 0;

break;

case ORIENTATION_PORTRAIT_INVERTED:

rotate = 270;

break;

case ORIENTATION_LANDSCAPE_INVERTED:

rotate = 180;

break;

}

Matrix matrix = new Matrix();

matrix.postRotate(rotate);

bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);

mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);

savePhoto(mutableBitmap);

dialog.dismiss();

flag = 0;

finish();

}

在照相機 Activity 中的onresume中,以磅為單位的向左方向:mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {

@SuppressWarnings("deprecation")

@Override

public void onOrientationChanged(int orientation) {

//determine our orientation based on sensor response

int lastOrientation = mOrientation;

Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

int rotation = getWindowManager().getDefaultDisplay().getRotation();

System.out.println(rotation+"");

if (display.getOrientation()!= Surface.ROTATION_0) {//landscape oriented devices

System.out.println("LANDSCAPE");

if (orientation> = 315 || orientation <45) {

if (mOrientation!= ORIENTATION_LANDSCAPE_NORMAL) {

mOrientation = ORIENTATION_LANDSCAPE_NORMAL;

}

} else if (orientation <315 && orientation> = 225) {

if (mOrientation!= ORIENTATION_PORTRAIT_INVERTED) {

mOrientation = ORIENTATION_PORTRAIT_INVERTED;

}

} else if (orientation <225 && orientation> = 135) {

if (mOrientation!= ORIENTATION_LANDSCAPE_INVERTED) {

mOrientation = ORIENTATION_LANDSCAPE_INVERTED;

}

} else if (orientation <135 && orientation> 45) {

if (mOrientation!= ORIENTATION_PORTRAIT_NORMAL) {

mOrientation = ORIENTATION_PORTRAIT_NORMAL;

}

}

} else {//portrait oriented devices

System.out.println("PORTRAIT");

if (orientation> = 315 || orientation <45) {

if (mOrientation!= ORIENTATION_PORTRAIT_NORMAL) {

mOrientation = ORIENTATION_PORTRAIT_NORMAL;

}

} else if (orientation <315 && orientation> = 225) {

if (mOrientation!= ORIENTATION_LANDSCAPE_NORMAL) {

mOrientation = ORIENTATION_LANDSCAPE_NORMAL;

}

} else if (orientation <225 && orientation> = 135) {

if (mOrientation!= ORIENTATION_PORTRAIT_INVERTED) {

mOrientation = ORIENTATION_PORTRAIT_INVERTED;

}

} else if (orientation <135 && orientation> 45) {

if (mOrientation!= ORIENTATION_LANDSCAPE_INVERTED) {

mOrientation = ORIENTATION_LANDSCAPE_INVERTED;

}

}

}

}

};

如果你還需要保存並使用你旋轉的圖像,請將照片函數保存並使用以下我的答案:

savePhoto函數:public void savePhoto(Bitmap bmp) {

imageFileFolder = new File(Environment.getExternalStorageDirectory(),

cc.getDirectoryName());

imageFileFolder.mkdir();

FileOutputStream out = null;

Calendar c = Calendar.getInstance();

String date = fromInt(c.get(Calendar.MONTH))

+ fromInt(c.get(Calendar.DAY_OF_MONTH))

+ fromInt(c.get(Calendar.YEAR))

+ fromInt(c.get(Calendar.HOUR_OF_DAY))

+ fromInt(c.get(Calendar.MINUTE))

+ fromInt(c.get(Calendar.SECOND));

imageFileName = new File(imageFileFolder, date.toString() +".jpg");

try {

out = new FileOutputStream(imageFileName);

bmp.compress(Bitmap.CompressFormat.JPEG, 70, out);

out.flush();

out.close();

scanPhoto(imageFileName.toString());

out = null;

} catch (Exception e) {

e.printStackTrace();

}

}

scanPhoto函數:public void scanPhoto(final String imageFileName) {

geniusPath = imageFileName;

msConn = new MediaScannerConnection(MyClass.this,

new MediaScannerConnectionClient() {

public void onMediaScannerConnected() {

msConn.scanFile(imageFileName, null);

}

@Override

public void onScanCompleted(String path, Uri uri) {

msConn.disconnect();

}

});

msConn.connect();

}

SavePhotoTask類:class SavePhotoTask extends AsyncTask {

@Override

protected String doInBackground(byte[]... jpeg) {

File photo = new File(Environment.getExternalStorageDirectory(),

"photo.jpg");

if (photo.exists()) {

photo.delete();

}

try {

FileOutputStream fos = new FileOutputStream(photo.getPath());

fos.write(jpeg[0]);

fos.close();

} catch (java.io.IOException e) {

}

return (null);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值