在 android 中使用自定义相机拍照
Take a photo using custom camera in android
我不是要配置自定义相机,我只需要用它拍照。
我的 xml 中有一个 SurfaceView 和一个拍照按钮。我找到这种拍照方法:
mCamera.takePicture(null, null, mPicture);
m图片定义如下:
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
我需要的是拍摄图像的位图,以便在另一个 Activity 中使用它。
提前致谢!
> I asked my instructor about this question and he told me that the
> code below should work fine for you.
public class Custom_Camera
{
private Camera a;
private ab1 ab2;
/** Called when the activity is first created. */
@Override
public void onCreate(Total State) {
super.onCreate(Saved State);
setContentView(R.layout.main);
a = getCameraInstance();
ab1 = new ab2(this, a);
FrameLayout= (FrameLayout) findViewById(R.id.a_b2);
preview.addView(ab2);
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
}
});
}
private Camera a getCameraInstance() {
Camera a = null;
try {
camera = Camera.open();
} catch (Exception e) {
}
return camera a;
}
Picture = new Picture() {
public void taken(byte[] data, Camera camera) {
File = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
What I need for is a bitmap of the taken image in order to use it in another Activity. Thanks in advance!
您只需要对从相机接收到的字节数组进行解码,然后将其旋转到正确的方向即可。
获取相机显示方向:
private static int getCameraDisplayOrientation(int cameraId, Activity activity) {
int rotation = ((WindowManager)activity.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getRotation();
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + rotation) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - rotation + 360) % 360;
}
return result;
}
将字节数组解码为位图:
public static Bitmap decodeByteArray(byte[] data, float rotationDegree) {
try {
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
if (rotationDegree != 0) {
bm = createRotatedBitmap(bm, rotationDegree);
}
return bm;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
旋转位图:
public static Bitmap createRotatedBitmap(Bitmap bm, float rotation) {
Matrix matrix = new Matrix();
matrix.postRotate(rotation, bm.getWidth()/2, bm.getHeight()/2);
try {
Bitmap result = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
return result;
} catch (OutOfMemoryError e) {
return null;
}
}
我不是要配置自定义相机,我只需要用它拍照。
我的 xml 中有一个 SurfaceView 和一个拍照按钮。我找到这种拍照方法:
mCamera.takePicture(null, null, mPicture);
m图片定义如下:
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
我需要的是拍摄图像的位图,以便在另一个 Activity 中使用它。 提前致谢!
> I asked my instructor about this question and he told me that the
> code below should work fine for you.
public class Custom_Camera
{
private Camera a;
private ab1 ab2;
/** Called when the activity is first created. */
@Override
public void onCreate(Total State) {
super.onCreate(Saved State);
setContentView(R.layout.main);
a = getCameraInstance();
ab1 = new ab2(this, a);
FrameLayout= (FrameLayout) findViewById(R.id.a_b2);
preview.addView(ab2);
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
}
});
}
private Camera a getCameraInstance() {
Camera a = null;
try {
camera = Camera.open();
} catch (Exception e) {
}
return camera a;
}
Picture = new Picture() {
public void taken(byte[] data, Camera camera) {
File = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
What I need for is a bitmap of the taken image in order to use it in another Activity. Thanks in advance!
您只需要对从相机接收到的字节数组进行解码,然后将其旋转到正确的方向即可。
获取相机显示方向:
private static int getCameraDisplayOrientation(int cameraId, Activity activity) {
int rotation = ((WindowManager)activity.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getRotation();
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + rotation) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - rotation + 360) % 360;
}
return result;
}
将字节数组解码为位图:
public static Bitmap decodeByteArray(byte[] data, float rotationDegree) {
try {
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
if (rotationDegree != 0) {
bm = createRotatedBitmap(bm, rotationDegree);
}
return bm;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
旋转位图:
public static Bitmap createRotatedBitmap(Bitmap bm, float rotation) {
Matrix matrix = new Matrix();
matrix.postRotate(rotation, bm.getWidth()/2, bm.getHeight()/2);
try {
Bitmap result = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
return result;
} catch (OutOfMemoryError e) {
return null;
}
}