1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Camera prameters do not have any effect

Discussion in 'Software Development' started by stutech123, Nov 16, 2011.

Thread Status:
Not open for further replies.
  1. stutech123

    stutech123 Thread Starter

    Joined:
    Nov 16, 2011
    Messages:
    1
    The following is my code, i need to have a solarized effect but the parameter's not getting set.What could be the problem??

    package com.android;

    import java.io.IOException;
    import java.util.List;

    import android.app.Activity;
    import android.content.Intent;
    import android.content.res.Configuration;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.PixelFormat;
    import android.hardware.Camera;
    import android.hardware.Camera.PictureCallback;
    import android.hardware.Camera.Size;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Surface;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.Button;

    public class CameraActivity extends Activity implements SurfaceHolder.Callback{
    private Camera camera=null;
    private SurfaceHolder surfaceHolder = null;
    private boolean previewRunning = false;
    private Button btnDone,btnCapture,btnRetake;
    private Bitmap mBitmap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.camera_screen);
    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    surfaceHolder.setFixedSize(getWindow().getWindowManager()
    .getDefaultDisplay().getWidth(), getWindow().getWindowManager()
    .getDefaultDisplay().getHeight());

    btnCapture =(Button) findViewById(R.id.btnCapture);
    btnDone =(Button) findViewById(R.id.btnDone);
    btnDone.setVisibility(View.INVISIBLE);
    btnRetake =(Button) findViewById(R.id.btnRetake);
    btnRetake.setVisibility(View.INVISIBLE);

    btnCapture.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    btnDone.setVisibility(View.VISIBLE);
    btnRetake.setVisibility(View.VISIBLE);
    btnCapture.setVisibility(View.INVISIBLE);
    camera.takePicture(null, picCalBac, picCalBac);
    }
    });

    btnDone.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intent=new Intent(CameraActivity.this,BActivity.class).putExtra("mBitmap",mBitmap);
    startActivity(intent);
    finish();
    }
    });

    btnRetake.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    if (previewRunning) {
    camera.stopPreview();
    }
    Camera.Parameters parameters = camera.getParameters();
    parameters.setColorEffect(Camera.Parameters.EFFECT_SOLARIZE);
    camera.setParameters(parameters);
    try {
    camera.setPreviewDisplay(surfaceHolder);
    } catch (IOException e) {
    Log.d("IOException", e.getMessage());
    }
    camera.startPreview();
    previewRunning = true;
    btnDone.setVisibility(View.INVISIBLE);
    btnRetake.setVisibility(View.INVISIBLE);
    btnCapture.setVisibility(View.VISIBLE);
    }
    });
    }

    Camera.PictureCallback picCalBac = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
    if (data != null) {
    mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    }
    }
    };

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    if (previewRunning) {
    camera.stopPreview();
    }
    Camera.Parameters parameters = camera.getParameters();
    List<Size> sizes = parameters.getSupportedPreviewSizes();
    Size optimalSize = getOptimalPreviewSize(sizes, w, h);
    parameters.setPreviewSize(optimalSize.width, optimalSize.height);
    parameters.setColorEffect(Camera.Parameters.EFFECT_SOLARIZE);

    setCameraDisplayOrientation(CameraActivity.this,0,camera);

    camera.setParameters(parameters);
    try {
    camera.setPreviewDisplay(surfaceHolder);
    } catch (IOException e) {
    Log.d("IOException", e.getMessage());
    }
    camera.startPreview();
    previewRunning = true;
    }

    public static void setCameraDisplayOrientation(Activity activity,
    int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info =
    new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0: degrees = 0; break;
    case Surface.ROTATION_90: degrees = 90; break;
    case Surface.ROTATION_180: degrees = 180; break;
    case Surface.ROTATION_270: degrees = 270; break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
    result = (info.orientation + degrees) % 360;
    result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
    result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
    }


    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.05;
    double targetRatio = (double) w / h;
    if (sizes == null) return null;

    Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    // Try to find an size match aspect ratio and size
    for (Size size : sizes) {
    double ratio = (double) size.width / size.height;
    if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
    if (Math.abs(size.height - targetHeight) < minDiff) {
    optimalSize = size;
    minDiff = Math.abs(size.height - targetHeight);
    }
    }

    // Cannot find the one match the aspect ratio, ignore the requirement
    if (optimalSize == null) {
    minDiff = Double.MAX_VALUE;
    for (Size size : sizes) {
    if (Math.abs(size.height - targetHeight) < minDiff) {
    optimalSize = size;
    minDiff = Math.abs(size.height - targetHeight);
    }
    }
    }
    return optimalSize;
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
    camera = Camera.open(0);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
    camera.stopPreview();
    previewRunning = false;
    camera.release();
    }
    }
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1027072

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice