[英]Getting a black screen after save photo - Android Camera
I'm developing an application for Android that captures max fps and saves to the SD card. 我正在开发用于Android的应用程序,该应用程序可以捕获最大fps并保存到SD卡。
The problem is that the saved photo is a black screen, and I can't understand why. 问题是保存的照片是黑屏,我不明白为什么。
Can anyone tell me where is the problem? 谁能告诉我问题出在哪里?
The code where I do this: 我执行此操作的代码:
public class PhotoFragment extends Fragment {
private Camera cam;
private CameraPreview camPreview;
private boolean recording = false;
private ArrayList<byte[]> fotos;
private ArrayList<String> tempos;
private Thread thread;
public PhotoFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photofinish, container, false);
cam = getCameraInstance();
if(cam != null) {
cam.setDisplayOrientation(90);
// set Camera parameters
Camera.Parameters cameraParameters = cam.getParameters();
//set color efects to none
cameraParameters.setColorEffect(Camera.Parameters.EFFECT_NONE);
//set antibanding to none
if (cameraParameters.getAntibanding() != null) {
cameraParameters.setAntibanding(Camera.Parameters.ANTIBANDING_OFF);
}
// set white ballance
if (cameraParameters.getWhiteBalance() != null) {
cameraParameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT);
}
//set flash
if (cameraParameters.getFlashMode() != null) {
cameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
//set zoom
if (cameraParameters.isZoomSupported()) {
cameraParameters.setZoom(0);
}
//set focus mode
cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
List<Size> sizes = cameraParameters.getSupportedPictureSizes();
Camera.Size size = sizes.get(0);
cameraParameters.setPictureSize(size.width, size.height);
cam.setParameters(cameraParameters);
fotos = new ArrayList<byte[]>();
tempos = new ArrayList<String>();
camPreview = new CameraPreview(this.getActivity(), cam);
FrameLayout preview = (FrameLayout) rootView.findViewById(R.id.camera_preview);
preview.addView(camPreview);
TextView startRecording = (TextView) rootView.findViewById(R.id.start_record_button);
startRecording.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
if(!recording)
{
recording = true;
Size previewSize = cam.getParameters().getPreviewSize();
int dataBufferSize=(int)(previewSize.height*previewSize.width*(ImageFormat.getBitsPerPixel(cam.getParameters().getPreviewFormat())/8.0));
thread.start();
cam.addCallbackBuffer(new byte[dataBufferSize]);
cam.addCallbackBuffer(new byte[dataBufferSize]);
cam.addCallbackBuffer(new byte[dataBufferSize]);
cam.setPreviewCallbackWithBuffer(new PreviewCallback() {
public void onPreviewFrame(byte[] imageData, Camera arg1) {
try {
fotos.add(imageData);
tempos.add(new SimpleDateFormat("HH_mm_ss_SSS", Locale.getDefault()).format(new Date()));
} catch(Exception e) {
System.out.println("ERRO: " + e);
}
}
});
}
else
{
recording = false;
try {
thread.join();
} catch (Exception e) {
}
}
}
});
thread = new Thread(new Runnable() {
public void run() {
while(recording) {
if(fotos.size()>0 && tempos.size()>0)
{
File pictureFile = getOutputMediaFile(1, tempos.get(0));
if (pictureFile == null){
System.out.println("Error creating media file, check storage permissions: ");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(fotos.get(0));
fos.close();
pictureFile = null;
cam.addCallbackBuffer(fotos.get(0));
fotos.remove(0);
tempos.remove(0);
} catch (FileNotFoundException e) {
System.out.println("ERRO FILE NOT FOUND! : " + e);
} catch (IOException e) {
System.out.println("ERRO IOException!");
}
}
}
}
});
}
else
{
Toast.makeText(getActivity(), "Camera not available", Toast.LENGTH_LONG).show();
}
return rootView;
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
}
catch (Exception e){
}
return c;
}
@Override
public void onDestroyView() {
super.onDestroyView();
cam.release();
}
private static File getOutputMediaFile(int type, String timeStamp){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "L_P");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
System.out.println("L_P failed to create directory");
return null;
}
}
String timeStampDay = new SimpleDateFormat("ddMMyyyy", Locale.getDefault()).format(new Date());
new File(mediaStorageDir.getPath() + File.separator + timeStampDay).mkdirs();
File mediaFile;
if (type == 1){
mediaFile = new File(mediaStorageDir.getPath() + File.separator + timeStampDay + File.separator + "IMG_"+ timeStamp + ".jpg");
} else if(type == 2) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
} }
You are claiming that the file is a JPEG. 您声称该文件是JPEG。 However, you have done nothing to convert the image to be a JPEG.
但是,您没有做任何将图像转换为JPEG的操作。 Preview frames, by default, are not JPEG, but are in NV21 format.
默认情况下,预览帧不是JPEG,而是NV21格式。 Use
getSupportedPreviewFormats()
to see if JPEG previews are possible, then use setPreviewFormat()
to request JPEG previews. 使用
getSupportedPreviewFormats()
查看是否可以进行JPEG预览,然后使用setPreviewFormat()
请求JPEG预览。
And, as I noted in your previous question, do NOT have two threads working with an ArrayList
. 并且,正如我在上一个问题中指出的那样, 没有两个线程与
ArrayList
一起工作。 Also, do not have your background thread busy-wait looking constantly for an image to show up on an ArrayList
. 此外, 没有你的后台线程忙等不断寻找的图像显示在一个
ArrayList
。 Use a LinkedBlockingQueue
or something else that is thread-safe and allows the background thread to block while waiting for an image. 使用
LinkedBlockingQueue
或其他线程安全的线程,并允许后台线程在等待图像时阻塞。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.