Android:我无法在 SurfaceView 上使用前置摄像头拍照(三星 Galaxy S GT-i9000)
我的三星 Galaxy S 前置摄像头出现绿屏,但预览是正确的。使用后置摄像头我可以拍照。
问题出在哪里?
这是我的 PictureCallback:
public void onIvButtonShutterClick(View view) {
PictureCallback pictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
File picture = new File(My_Camera.this.getFilesDir()
+ "/bild.jpg");
FileOutputStream pictureOut = new FileOutputStream(picture);
pictureOut.write(data);
pictureOut.flush();
pictureOut.close();
Toast.makeText(
My_Camera.this,
getResources().getString(
R.string.tx_my_camera_save)
+ "\n" + picture.getAbsolutePath(),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
//mCamera.startPreview(); // Preview wird weiter ausgeführt
}
};
mCamera.takePicture(null, null, pictureCallback);
}
我通过以下方式访问前置摄像头:
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
mParameters = mCamera.getParameters();
mParameters.set("camera-id", 2);
mParameters.setPictureFormat(PixelFormat.JPEG);
mCamera.setDisplayOrientation(270);
mCamera.setParameters(mParameters);
mCamera.startPreview();
}
I get a green screen with the front camera from samsung galaxy s, but the preview is correct. With the Back-Camera can I make photos.
Where is the problem?
This is my PictureCallback:
public void onIvButtonShutterClick(View view) {
PictureCallback pictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
File picture = new File(My_Camera.this.getFilesDir()
+ "/bild.jpg");
FileOutputStream pictureOut = new FileOutputStream(picture);
pictureOut.write(data);
pictureOut.flush();
pictureOut.close();
Toast.makeText(
My_Camera.this,
getResources().getString(
R.string.tx_my_camera_save)
+ "\n" + picture.getAbsolutePath(),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
//mCamera.startPreview(); // Preview wird weiter ausgeführt
}
};
mCamera.takePicture(null, null, pictureCallback);
}
I access on the front camera with:
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
mParameters = mCamera.getParameters();
mParameters.set("camera-id", 2);
mParameters.setPictureFormat(PixelFormat.JPEG);
mCamera.setDisplayOrientation(270);
mCamera.setParameters(mParameters);
mCamera.startPreview();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,但从未找到真正的解决方案。
我最终使用的是从相机预览中抓取帧。这有一些问题——比如,图片更有可能变得模糊。
抓取预览帧的相关方法是
Camera.setOneShotPreviewCallback
。I had the same problem but never found a real solution.
What I ended up using was to grab frames from the camera preview. This has some issues – like, pictures are much more likely to be blurry.
The relevant method for grabbing preview frames is
Camera.setOneShotPreviewCallback
.我认为问题可能出在结果的处理上。我猜您正在将最初捕获的包含原始图像数据的字节数组发送到具有 jpg 扩展名的文件,但该字节数组不在 jpg 数据中(即缺少标头且具有未压缩的内容)。
这是我拥有的一些代码(您看到的 MeToo 项目)中的相关片段,imageData 是原始内容:
希望这有帮助......
I think the problem might be the processing of the result. I'm guessing you are sending the originally captured byte array containing the raw image data to a file with jpg extension, but the byte array is not in jpg data (i.e. missing headers and with uncompressed content).
Here's a relevant piece from some code I have (the MeToo project that you saw), imageData being the raw content:
Hope this helps...
您是否尝试设置此参数:
它对我有帮助,但在摩托罗拉 Droid 上引起问题。
Did you try to set this parameter:
It helped me, but on Motorola Droid causing problems.