将位图缩放到较低分辨率后的低质量

时间:2013-09-30 17:53:35

标签: android bitmap

我正在尝试缩小位图,但边缘不像原始图像那样清晰,其模糊不清

我已查看以下链接

Bad image quality after resizing/scaling bitmap

请帮忙

 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);

如果我尝试缩放

,viewCapture元素会变得模糊

1 个答案:

答案 0 :(得分:0)

让我试着帮助你。

首先,如果您所需的相机捕获图片尺寸为600 * 400或类似尺寸 我会说相机参数设置为你想要的尺寸(相机支持),你会得到相机拍照方法中的小图像

注意:但请确保首先需要检查设备相机支持的图片尺寸。然后设置其中一个。

以下是我为Galaxy Nexus测试的一个例子

  

Galaxy Nexus相机支持的图片尺寸

     

支持的图片大小。宽度:2592 *高度:1944

     

支持的图片大小。宽度:2592 *高度:1728

     

支持的图片大小。宽度:2592 *高度:1458

     

支持的图片大小。宽度:2048 *高度:1536

     

支持的图片大小。宽度:1600 *高度:1200

     

支持的图片大小。宽度:1280 *高度:1024

     

支持的图片大小。宽度:1152 *高度:864

     

支持的图片大小。宽度:1280 *高度:960

     

支持的图片大小。宽度: 640 *高度:480

     

支持的图片大小。宽度:320 *高度:240

下面是示例代码将帮助您

<强> 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;
    }
}

请告诉我你的评论。