0

I am trying to scale down a Bitmap but the edges are not clear as the original image, its little blurred

I have checked out the below link

Bad image quality after resizing/scaling bitmap

Please help

 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);
           Matrix m=new Matrix();
           m.postRotate(90);
           //m.postScale((float)0.5,(float) 0.5);
           //Added for merging
           //Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
           Bitmap mutableBitmap = Bitmap.createBitmap(bitmap,0,0,600,400,m,false);

           Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);

           Canvas canvas = new Canvas(mutableBitmap);
           View v=(View)relLay.getParent();
           v.setDrawingCacheEnabled(true);
           v.buildDrawingCache();
           Options options = new BitmapFactory.Options();
           options.inScaled = false;
           //options.inJustDecodeBounds=true;
           options.inSampleSize=2;
           //Bitmap viewCapture = v.getDrawingCache().copy(Bitmap.Config.ARGB_8888, true);
          // Bitmap newImage = Bitmap.createScaledBitmap(viewCapture, viewCapture.getWidth()/2, viewCapture.getHeight()/2, true);
           ByteArrayOutputStream stream = new ByteArrayOutputStream();
           (v.getDrawingCache().copy(Bitmap.Config.ARGB_8888, true)).compress(Bitmap.CompressFormat.PNG, 100, stream);
           byte[] byteArray = stream.toByteArray();
           Bitmap viewCapture= BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options);


           v.setDrawingCacheEnabled(false);
           Rect src = new Rect(0, 0, viewCapture.getWidth(), viewCapture.getHeight());
           Log.d("TEST",""+viewCapture.getWidth()+" "+viewCapture.getHeight());
           //Destination RectF sized to the camera picture
            Rect dst = new Rect(0, 0, mutableBitmap.getWidth(), mutableBitmap.getHeight());
            Log.d("Test",""+mutableBitmap.getWidth()+" "+mutableBitmap.getHeight());
            canvas.drawBitmap(viewCapture, src, dst, paint);
           // Bitmap newImage = Bitmap.createScaledBitmap(viewCapture, 400,600, true);
            mutableBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

The viewCapture element gets blurred out if I try to scale

Community
  • 1
  • 1
Astr0
  • 81
  • 1
  • 7
  • You're scaling it down with `sampleSize 2` and then scaling again to `600x400`. Blur happens when you scale a scaled image. A screenshot might help determine if this is the usual amount of blurring or something else. – Geobits Sep 30 '13 at 18:14
  • I have removed it and checked but the blurring remained. I mean the picture is not clear as the original one(without scaling) – Astr0 Sep 30 '13 at 18:45
  • I'm not sure which part you're saying you removed, but I think screenshots are still a good idea. – Geobits Sep 30 '13 at 18:48

1 Answers1

1

Let me try to help you.

First of all, If you your desired Camera Captured Picture size is 600*400 or similar size then I would say set the camera param for your desired size (which is supported by camera) and you will get the small image in Camera's Picture taken method

Note : but make sure first you need to check what ate picture sizes supported by Device camera. then set one of them.

Here is the one example I tested for Galaxy Nexus

Galaxy Nexus Camera Supported Picture sizes

Supported Picture Size. Width: 2592 * height : 1944

Supported Picture Size. Width: 2592 * height : 1728

Supported Picture Size. Width: 2592 * height : 1458

Supported Picture Size. Width: 2048 * height : 1536

Supported Picture Size. Width: 1600 * height : 1200

Supported Picture Size. Width: 1280 * height : 1024

Supported Picture Size. Width: 1152 * height : 864

Supported Picture Size. Width: 1280 * height : 960

Supported Picture Size. Width: 640 * height : 480

Supported Picture Size. Width: 320 * height : 240

below is the sample code will help you

CameraActivity.java

public class CameraActivity extends Activity implements SurfaceHolder.Callback,
        OnClickListener 
{

Camera camera;
SurfaceHolder surfaceHolder;
boolean previewing = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
          surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)    
    {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) 
    {
        // TODO Auto-generated method stub
        Log.v(TAG, "surfaceCreated get called");
        camera = Camera.open();
        camera.setDisplayOrientation(90); //to get portrait display


        if (camera != null) {
            try {

                //Here is the main logic
                // We are setting camera parameters as our desired picture size

                Camera.Parameters parameters = camera.getParameters();              
                List<Size> sizes = parameters.getSupportedPictureSizes();

                Camera.Size result = null;
                for (int i=0;i<sizes.size();i++)
                {
                    result = (Size) sizes.get(i);
                    Log.i("PictureSize", "Supported Size. Width: " + result.width + "height : " + result.height); 

                    if(result.width == 640)
                    {
                        parameters.setPreviewSize(result.width, result.height);//640*480
                            parameters.setPictureSize(result.width, result.height);

                        //Now if camera support for 640*480 pictures size you will get captured image as same size
                    }
                    else
                    {
                        //to do here

                    }
                }  

                camera.setParameters(parameters);
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
        Log.v(TAG, "surfaceDestroyed get called");
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }
}

Let me know your comment in this.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135