0

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

我已经检查了下面的链接

调整大小/缩放位图后图像质量不佳

请帮忙

 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 元素会变得模糊

4

1 回答 1

1

让我试着帮助你。

首先,如果您想要的相机捕获的图片尺寸是 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;
    }
}

让我知道你对此的评论。

于 2013-10-01T07:56:09.017 回答