public void setViewSize ( Dimension size ) { if ( size == null ) { throw new IllegalArgumentException ( "Resolution cannot be null!" ) ; } if ( open . get ( ) ) { throw new IllegalStateException ( "Cannot change resolution when webcam is open, please close it first" ) ; } // check if new resolution is the same as current one Dimension current = getViewSize ( ) ; if ( current != null && current . width == size . width && current . height == size . height ) { return ; } // check if new resolution is valid Dimension [ ] predefined = getViewSizes ( ) ; Dimension [ ] custom = getCustomViewSizes ( ) ; assert predefined != null ; assert custom != null ; boolean ok = false ; for ( Dimension d : predefined ) { if ( d . width == size . width && d . height == size . height ) { ok = true ; break ; } } if ( ! ok ) { for ( Dimension d : custom ) { if ( d . width == size . width && d . height == size . height ) { ok = true ; break ; } } } if ( ! ok ) { StringBuilder sb = new StringBuilder ( "Incorrect dimension [" ) ; sb . append ( size . width ) . append ( "x" ) . append ( size . height ) . append ( "] " ) ; sb . append ( "possible ones are " ) ; for ( Dimension d : predefined ) { sb . append ( "[" ) . append ( d . width ) . append ( "x" ) . append ( d . height ) . append ( "] " ) ; } for ( Dimension d : custom ) { sb . append ( "[" ) . append ( d . width ) . append ( "x" ) . append ( d . height ) . append ( "] " ) ; } throw new IllegalArgumentException ( sb . toString ( ) ) ; } LOG . debug ( "Setting new resolution {}x{}" , size . width , size . height ) ; device . setResolution ( size ) ; }
Set new view size . New size has to exactly the same as one of the default sized or exactly the same as one of the custom ones .