Android Camera :- camera got hanged and screen freeze when take a picture on Samsung Galaxy on7 Pro

  • Replies:0
Pankaj Dev
  • Forum posts: 1

Apr 28, 2017, 6:50:43 AM via Website

I m new in Android Development , i am using these two classes in java to create a custom camera, when i click the capure Button then application freezes and camera app hangs.that code is abslutely work on Lenovo note K6, Sony Xperia. But does not work on Samsung Galaxy On7 Pro

Code is Here,

BackCameraActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_back_camera);
captureButton = (Button) findViewById(R.id.back_capture);
FrameLayout preview = (FrameLayout) findViewById(R.id.backCameraPreview);
con = getApplicationContext();
try {
releaseCamera();
mCamera = getCameraInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPreview = new CameraPreview(this, mCamera);

preview.addView(mPreview);

captureButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        captureButton.setEnabled(false);
        mCamera.takePicture(null, null, mPicture);
    }
});

}
Bitmap bitmap;
private PictureCallback mPicture = new PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {

    System.gc();
    bitmap = null;
    BitmapWorker task = new BitmapWorker(data);
    task.execute(0);
}

};
class BitmapWorker extends AsyncTask {
private final WeakReference dataf;
private int data = 0;

public BitmapWorker(byte[] imgdata) {
    dataf = new WeakReference<byte[]>(imgdata);
}

@Override
protected Bitmap doInBackground(Integer... params) {
    data = params[0];
    ResultActivity(dataf.get());
    return mainBitmap;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
    if (mainBitmap != null) {

        Intent i = new Intent();
        i.putExtra("BitmapImage", mainBitmap);
        setResult(-1, i);
        finish();
    }
}

}

public void ResultActivity(byte[] data) {
mainBitmap = null;
mainBitmap = decodeSampledBitmapFromResource(data, 200, 200);
mainBitmap=RotateBitmap(mainBitmap,270);
mainBitmap=flip(mainBitmap);
}

public static Bitmap decodeSampledBitmapFromResource(byte[] data,int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}

public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
    if (width > height) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    } else {
        inSampleSize = Math.round((float) width / (float) reqWidth);
    }
}
return inSampleSize;

}

public static Camera getCameraInstance() {
Camera c = null;
Log.d("No of cameras", Camera.getNumberOfCameras() + "");
for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
CameraInfo camInfo = new CameraInfo();
Camera.getCameraInfo(camNo, camInfo);
if (camInfo.facing == (CameraInfo.CAMERA_FACING_BACK)) {
c = Camera.open(camNo);
c.setDisplayOrientation(90);
}
}
return c;
}

private void releaseCamera() {
if (mCamera != null) {
try {
mCamera.stopPreview();
}
catch (Exception e){
}
mCamera.release();
mCamera = null;
}
}

public static Bitmap RotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
source.getHeight(), matrix, true);
}

Bitmap flip(Bitmap d)
{
Matrix m = new Matrix();
m.preScale(-1, 1);
Bitmap src = d;
Bitmap dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
dst.setDensity(DisplayMetrics.DENSITY_DEFAULT);
return dst;
}

CameraPreview.java

public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {

try {
    mCamera.setPreviewDisplay(holder);
    mCamera.startPreview();
} catch (IOException e) {
    Log.d("", "Error setting camera preview: " + e.getMessage());
}

}

public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.release();
}
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

if (mHolder.getSurface() == null){
    return;
}
try {
    mCamera.stopPreview();
} catch (Exception e){
}
try {
    mCamera.setPreviewDisplay(mHolder);
    mCamera.startPreview();

} catch (Exception e){
    Log.d("", "Error starting camera preview: " + e.getMessage());
}

}

Kindly help me

Thanks & Regards,
Pankaj Dev

— modified on Apr 28, 2017, 6:52:09 AM

Reply