在android中使用自定义相机拍照
Posted
技术标签:
【中文标题】在android中使用自定义相机拍照【英文标题】:Take a photo using custom camera in android 【发布时间】:2017-08-05 05:33:47 【问题描述】:我不是要配置自定义相机,我只需要用它拍照。
我的 xml 中有一个 SurfaceView 和一个拍照按钮。我找到这个方法来拍照:
mCamera.takePicture(null, null, mPicture);
mPicture 定义如下:
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 中使用它。 提前致谢!
【问题讨论】:
为什么要显示所有代码!无关紧要。您的设备中有一个 jpg 文件,并且您想要它的位图就是您想要的。就这样。究竟是什么问题?告诉其他活动该文件的路径。 【参考方案1】:我需要一张拍摄图像的位图,以便在另一个 Activity 中使用它。提前致谢!
您只需要解码从相机接收到的字节数组,然后将其旋转到正确的方向。
获取相机显示方向:
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;
【讨论】:
【参考方案2】:> 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;
【讨论】:
另一个人在这里问过这个问题:link以上是关于在android中使用自定义相机拍照的主要内容,如果未能解决你的问题,请参考以下文章