Hello everyone Im working about screenshot while camera is on preview and I found it here Converting YUV->RGB(Image processing)->YUV during onPreviewFrame in android?. I did everything what it said in here but still nothing happens. Sometimes it returns an ArrayIndexOutOfBounce and some how save a poor image and sometimes not and crashes. I don't have a background on camera and image manipulation so please help me. Below is the image it capture and its code.
private final void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {
final int frameSize = width * height;
for (int j = 0, yp = 0; j < height; j++) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y = (0xff & ((int) yuv420sp[yp])) - 16;
if (y < 0) y = 0;
if ((i & 1) == 0) {
try{
v = (0xff & yuv420sp[uvp++]) - 128;
u = (0xff & yuv420sp[uvp++]) - 128;
}catch(Exception e){
//Log.d("uvp",e.getMessage());
}
}
int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
if (r < 0) r = 0; else if (r > 262143) r = 262143;
if (g < 0) g = 0; else if (g > 262143) g = 262143;
if (b < 0) b = 0; else if (b > 262143) b = 262143;
rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
}
}
}
public void screenShot(int widthParams, int heightParams)
{
AndroidLogger.log("Screen shot on androidCamera control!!");
AndroidLogger.log("Screen shot width is " + widthParams + " and height is " +heightParams);
final int widthScreenShot = widthParams;
final int heightScreenShot = heightParams;
camera.getParameters().setPreviewFormat(ImageFormat.RGB_565);
AndroidLogger.log("The image format " + camera.getParameters().getPreviewFormat());
previewCallback = new PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// TODO Auto-generated method stub
if(pictureManipulationThread == null)
{
pictureManipulationThread = new PictureManipulationThread();
pictureManipulationThread.start();
}
final byte[] fData = data;
AndroidLogger.log("Before decode size of fData " +fData.length);
AndroidLogger.log("The image format " + camera.getParameters().getPreviewFormat());
pictureManipulationThread.setRunnable(new Runnable() {
@Override
public void run() {
int rgb[] = new int[(widthScreenShot * heightScreenShot)];
Bitmap bitMap = Bitmap.createBitmap(widthScreenShot, heightScreenShot, Bitmap.Config.ARGB_8888);
AndroidLogger.log("Before decode size of fData " +fData.length);
decodeYUV420SP(rgb, fData, widthScreenShot, heightScreenShot);
bitMap.setPixels(rgb, 0, widthScreenShot, 0, 0, widthScreenShot, heightScreenShot);
//final Bitmap bMap = bitMap;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String fileName = formatter.format(new java.util.Date())+".jpg";
fileName ="DCIM/LululoloCamera/"+fileName;
bitMap.compress(CompressFormat.JPEG,90, Gdx.files.external(fileName).write(false));
AndroidLogger.log("Compressed");
bitMap.recycle();
}
});
}
};
this.camera.setPreviewCallback(previewCallback);
}
Heres the link for the image https://www.facebook.com/photo.php?fbid=1588086984751570&set=a.1578974695662799.1073741830.100006508501956&type=1&theater
0 comments:
Post a Comment